2

I can't insert a value in field text in html formulary using Selenium Python:

I have this HTML:

<div data-react-toolbox="input" class="_2dBwA"><input type="text" placeholder="Endereço de e-mail" class="_2WvFs" role="input"><span class="fT1WI"></span></div>

and this XPath:

(Copy Xpath) //*[@id="root"]/div/div[2]/div[2]/div/input

and this:

(Copy outerHTML) <input type="text" placeholder="Endereço de e-mail" class="_2WvFs" role="input">

I did it, but dont worked:

[In]: login_name = 'Cassandra'
[In]: insert_login_name = driver.find_element_by_xpath('//input[@id="root"]')
[In]: insert_login_name.send_keys(login_name);

[Out]: NoSuchElementException:  Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@id="root"]"}

After entering the text in this text field, the result would be in html 'values' = 'Cassandra'

<div data-react-toolbox="input" class="_2dBwA"><input type="text" placeholder="Endereço de e-mail" class="_2WvFs _3QmiH" role="input" value='Cassandra'><span class="fT1WI"></span></div>

What can i do? I'm new in that. Thanks

3
  • The error message is pretty clear: selenium is unable to find the element at the given xpath. I suggest using find_element_by_id('root') instead of trying to use an xpath. Commented Jan 13, 2020 at 20:33
  • Don't work. insert_login_name = driver.find_elements_by_id("root") and after i do it insert_login_name.send_keys('Cassandra'); THE RESULT was AttributeError: 'list' object has no attribute 'send_keys' Commented Jan 13, 2020 at 21:13
  • Check if there any iframe present above the input element.It seems like an iframe there? Commented Jan 13, 2020 at 21:23

4 Answers 4

3

The desired element is a ReactJS enabled element so to send a character sequence with in the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[data-react-toolbox='input']>input[placeholder='Endereço de e-mail'][type='text']"))).send_keys(login_name)
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-react-toolbox='input']/input[@placeholder='Endereço de e-mail' and @type='text']"))).send_keys(login_name)
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Update

Seems it was a locale issue. Changing the value of placeholder attribute from Endereço de e-mail to E-mail address works perfecto.

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[data-react-toolbox='input']>input[placeholder*='mail'][type='text']"))).send_keys(login_name)
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-react-toolbox='input']/input[contains(@placeholder, 'mail') and @type='text']"))).send_keys(login_name)
    

Reference

You can find a relevant detailed discussion in:

Sign up to request clarification or add additional context in comments.

2 Comments

@Gizélly Checkout the updated answer and let me know the status.
it works after update xpath! Thanks,i don't understand yesterday, but today i'm understand your work. Thanks for your help!
2

Induce WebDriverWait and element_to_be_clickable() and following css selector.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver=webdriver.Chrome()
driver.get("https://www.atlasgov.com/login")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'div[data-react-toolbox="input"] >input[placeholder="E-mail address"][role="input"]'))).send_keys("Cassandra")

Browser snapshot:

enter image description here


Updated Xpath.


WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//div[@data-react-toolbox="input" and @class="_2dBwA"]/input[@role="input"]'))).send_keys("Cassandra")

OR

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'(//div[@data-react-toolbox="input"]//input[@role="input"])[1]'))).send_keys("Cassandra")

4 Comments

Don't work.. TimeoutException: Message: -------TimeoutException T
The link you have provided it is working in UK location.wait I will provide you another solution.
@Gizélly : Try updated xpath and let me know the status?
I have updated another xpath.use that one.That is more convenient.
0

The error message is pretty clear: selenium is unable to find the element at the given xpath. Since you have the element's id just use it directly instead of an xpath.

driver.find_element_by_id('root')

4 Comments

Don't work. insert_login_name = driver.find_elements_by_id("root") and after i do it insert_login_name.send_keys('Cassandra'); THE RESULT was AttributeError: 'list' object has no attribute 'send_keys' –
@Gizélly Double check what you typed. It isn't the same as what I put in my answer here.
driver.find_elements_by_id("root") returns '[]'
@Gizélly There are two issues. 1. find_elements... returns a list of elements, not a single element. This is why you get the error. You can change this to find_element... instead, but then you will get the same error message as before that the element is not found. 2. There is no element with an id of root. This is probably related to your original error. You need to find the correct selector for the element you want. Your original xpath is incorrect.
0

It is issue about wrong locator xpath. Although in given Html there is no element with ID as root but it seems there can be any parent node with ID as root Please try with given xpath based on provided html. Hope it will work:

driver.find_element_by_xpath("//input[@placeholder='Endereço de e-mail']");

3 Comments

Don't work: InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //input[@placeholder=‘Endereço de e-mail’] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//input[@placeholder=‘Endereço de e-mail’]' is not a valid XPath expression.
Those quotation characters are invalid. You should use ' and " instead of and .
Yes, I am typing with mobile keypad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.