KOB
KOB

Reputation: 4545

Cannot enter text into input field (Python + Selenium)

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.

Upvotes: 1

Views: 869

Answers (2)

KunduK
KunduK

Reputation: 33384

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

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193108

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
    

Upvotes: 2

Related Questions