Search code examples
pythonselenium-webdriverxpathcss-selectorswebdriverwait

ElementNotInteractableException: Message: Element is not reachable by keyboard error sending text to input field using Selenium and Python


from selenium import webdriver

browser = webdriver.Firefox() # Opens Firefox webbrowser
browser.get('https://protonmail.com/') # Go to www.protonmail.com website
loginButton = browser.find_element_by_css_selector('#bs-example-navbar-collapse-1 > ul > li:nth-child(8) > a') # Finds login button
loginButton.click()  # Clicks login button
browser.implicitly_wait(10) # wait until the site has fully loaded

usernameElem = browser.find_element_by_css_selector('#username') # Finds login element for email/username
usernameElem.send_keys('[email protected]') # Enters email

passwordElem = browser.find_element_by_css_selector('#password') # Finds login element for password
passwordElem.send_keys('password') # Enters password # Enters password

This code crashes at the following line:

usernameElem.send_keys('[email protected]')

The error message is:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <input id="username" class="w100 inputform-field"> is not reachable by keyboard

I would like to understand, what the problem is first. I give the browser time to load. What is the reason for this error? And second: how can I solve the problem?


Solution

  • The <input> field have a ancestor <label> as:

    <label class="inputform-container w100 inputform-container--bigger" for="username">
    

    Snapshot:

    for_username


    To send a character sequence to the element 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://protonmail.com/")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.action>a[href='https://mail.protonmail.com/login']"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "label[for='username']"))).send_keys('[email protected]')
      
    • Using XPATH:

      driver.get('https://protonmail.com/')
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='action']/a[@href='https://mail.protonmail.com/login']"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[@for='username']"))).send_keys('[email protected]')
      
    • 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:

    protonmail_username


    References

    You can find a couple of relevant detailed discussions in: