-1

I'm trying to learn selenium and I want to fill out a form and copying some tutorial . In this form there a text input with this code :

<input required="" autocomplete="off" placeholder="First Name" type="text" id="firstName" class=" mr-sm-2 form-control">

Following the tutorial this code should do it :

from selenium import webdriver as w
d=w.Chrome(executable_path="C:\\chromedriver.exe")
d.get("https://demoqa.com/automation-practice-form")
try :
    d.find_element_by_id('firstName').send_keys("TEST")
except:
    print('cant identify')

d.quit()

I think the problem is the find element syntax anyways what is the problem ?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

d.find_element("xpath",'//*[@id="firstName"]').send_keys("amine") d.find_element("id","lastName").send_keys("wannes")

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 19 '22 at 18:16
-1

To send a character sequence to the <input> field you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    driver.get('https://demoqa.com/automation-practice-form')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#firstName"))).send_keys("hamdi")
    
  • Using XPATH:

    driver.get('https://demoqa.com/automation-practice-form')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='firstName']"))).send_keys("hamdi")
    
  • 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
    
  • Browser Snapshot:

toolsQA

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    i know i can use another solution but why does my code not work ? – hamdi salim Jul 17 '22 at 00:30
  • Checkout the updated answer and let me know if any further questions. – undetected Selenium Jul 17 '22 at 00:36
  • d.find_element_by_id('firstName').send_keys("TEST") why is this not working ? seems that ide does not recognize it – hamdi salim Jul 17 '22 at 00:50
  • Reread the answer and the embedded links. – undetected Selenium Jul 17 '22 at 01:34
  • @hamdisalim Take a look at the source HTML for the page and it will become obvious why your code doesn't work: it's because the `input` which you're trying to locate doesn't exist. That `input` and the `form` element containing it are not present in the HTML when the page loads and your Selenium process starts interrogating the page. Those elements are created by JavaScript code _after_ the page has loaded, so you will need to wait around until they have appeared. – Conal Tuohy Jul 17 '22 at 01:59
  • nope u are wrong the problem isnt there , i asked a dev for help he said the function i used dosent work anymore for new versions so i used this instead – hamdi salim Jul 17 '22 at 23:50
  • d.find_element("xpath",'//*[@id="firstName"]').send_keys("amine") – hamdi salim Jul 17 '22 at 23:50