ElementNotInteractableException:消息:使用 Selenium 和 Python 将文本发送到输入字段时,键盘错误无法访问元素

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('firstName.lastName@protonmail.com') # Enters email

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

此代码在以下行崩溃:

usernameElem.send_keys('firstName.lastName@protonmail.com')

错误信息是:

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

我想了解下,先是什么问题。我给浏览器加载时间。这个错误的原因是什么?第二:我该如何解决这个问题?

<input> 字段的祖先 <label> 为:

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

快照:


发送一个字符序列到你需要归纳的元素WebDriverWait for the and you can use either of the following :

  • 使用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('firstName.lastName@protonmail.com')
    
  • 使用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('firstName.lastName@protonmail.com')
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:


参考资料

您可以在以下位置找到一些相关的详细讨论: