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

petezurich
  • 9,280
  • 9
  • 43
  • 57
Gizelly
  • 417
  • 2
  • 10
  • 24
  • 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. – Code-Apprentice Jan 13 '20 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' – Gizelly Jan 13 '20 at 21:13
  • Check if there any iframe present above the input element.It seems like an iframe there? – KunduK Jan 13 '20 at 21:23

4 Answers4

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
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")
KunduK
  • 32,888
  • 5
  • 17
  • 41
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')
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 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' – – Gizelly Jan 13 '20 at 21:16
  • @Gizélly Double check what you typed. It isn't the same as what I put in my answer here. – Code-Apprentice Jan 13 '20 at 21:17
  • driver.find_elements_by_id("root") returns '[]' – Gizelly Jan 13 '20 at 21:23
  • 1
    @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. – Code-Apprentice Jan 13 '20 at 23:10
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']");
KunduK
  • 32,888
  • 5
  • 17
  • 41
Muzzamil
  • 2,823
  • 2
  • 11
  • 23
  • 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. – Gizelly Jan 13 '20 at 21:13
  • 1
    Those quotation characters are invalid. You should use `'` and `"` instead of `”` and `‘`. – CEH Jan 13 '20 at 21:48
  • Yes, I am typing with mobile keypad – Muzzamil Jan 13 '20 at 21:49