Gizelly
Gizelly

Reputation: 457

How to insert a value in a field Text using Selenium?

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

Upvotes: 2

Views: 2969

Answers (4)

undetected Selenium
undetected Selenium

Reputation: 193108

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:

Upvotes: 3

KunduK
KunduK

Reputation: 33384

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")

Upvotes: 2

Muzzamil
Muzzamil

Reputation: 2881

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']");

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83537

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')

Upvotes: 0

Related Questions