首页 > 解决方案 > ElementNotInteractableException:消息:键盘无法访问元素

问题描述

我想使用 Selenium 输入的表单中有以下 JS 代码片段:

<oj-input-password id="idcs-signin-basic-signin-form-password" data-idcs-placeholder-translation-id="idcs-password-placeholder" class="oj-sm-12 oj-inputpassword oj-form-control oj-component oj-complete" value="{{password}}" placeholder="[[bundle('signin.password')]]" labelled-by="ui-id-2"><input data-oj-internal="" type="password" placeholder="Password" class="oj-inputpassword-input oj-component-initnode" id="idcs-signin-basic-signin-form-password|input"></oj-input-password>

我正在尝试输入密码:

password = browser.find_element_by_id('idcs-signin-basic-signin-form-password')
password.send_keys("my_password")

但后来我收到以下错误:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <oj-input-password id="idcs-signin-basic-signin-form-password" class="oj-sm-12 oj-inputpassword oj-form-control oj-component oj-complete"> is not reachable by keyboard

为什么会发生这种情况,解决方法是什么?

标签: pythonseleniumselenium-webdriver

解决方案


导入显式等待:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

以这种方式使用它:

wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable(
            (By.CSS_SELECTOR, "#idcs-signin-basic-signin-form-password")))

并使用 CSS 选择器而不是 id:

password = browser.find_element_by_css_selector("#idcs-signin-basic-signin-form-password")
password.send_keys("my_password")

#表示元素id。

另外,检查条件:

  • 元素不是 iframe
  • 元素不在影子 DOM 中。

推荐阅读