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.

2 Answers 2

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
    
Sign up to request clarification or add additional context in comments.

3 Comments

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 This is most shortest XPath tutorial to start with.
Of course I will, can't do it for another couple of minutes
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')

2 Comments

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: can you try that.

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.