1

I have the following snippet of HTML code on a web page:

<div class="f0n8F ">
    <label for="f395fbf9cde026" class="_9nyy2">Phone number, username, or email</label>
    <input class="_2hvTZ pexuQ zyHYP" id="f395fbf9cde026" aria-describedby="slfErrorAlert" aria-label="Phone number, username, or email" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="75" name="username" type="text" value="">
</div>

And I am trying to input text using the following code:

username_element = WebDriverWait(driver, 5).until(expected_conditions.visibility_of_element_located((By.ID, "f395fbf9cde026")))
username_element.send_keys('abc')

I keep getting a TimeOut exception, even when I try (By.CLASS, "class _2hvTZ pexuQ zyHYP"), (By.XPATH, "//*[@id=\"f1798b97d45a38\"]").

If I try By.NAME, "username", there is another element with the name 'username' on the previous page, and so 'abc' is entered on this previous page before the above lines of code are executed.

Notably, if I ever try driver.implicitly_wait(x), no wait ever occurs.

KOB
  • 4,084
  • 9
  • 44
  • 88

2 Answers2

2

The desired element is a dynamic element so to send character sequence 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, "input[aria-describedby='slfErrorAlert'][name='username']"))).send_keys("KOB")
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-describedby='slfErrorAlert' and @name='username']"))).send_keys("KOB")
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    This works perfectly, thank you for the explanation. Following from this, I know nothing about frontend and HTML - how did you come up with this xpath from the snippet I sent? What is the format for xpaths? – KOB Jun 13 '19 at 12:59
  • 1
    @KOB This is most shortest [XPath](https://www.w3schools.com/xml/xpath_intro.asp) tutorial to start with. – undetected Selenium Jun 13 '19 at 13:02
  • Of course I will, can't do it for another couple of minutes – KOB Jun 13 '19 at 13:02
0

Try use the xpath which should be unique.

username_element = WebDriverWait(driver, 5).until(expected_conditions.element_to_be_clickable((By.XPATH, "//input[@name='username'][@aria-describedby='slfErrorAlert']")))
username_element.send_keys('abc')
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • See the last section of my question. This works, but the previous page before this one is opened also has an element with the name 'username', and the text is entered here before the new page is opened. – KOB Jun 13 '19 at 12:53
  • @KOB: can you try that. – KunduK Jun 13 '19 at 12:56