python - 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
为什么会发生这种情况,解决方法是什么?
解决方案
导入显式等待:
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 中。
推荐阅读
- angular - 在位置 0 的 json 中获取错误请求和意外令牌
- python - 在 python 环境中运行 python 脚本的 Bash 脚本在 ubuntu 命令行上运行,但不在 crontab 中
- c# - '>' 是一个意外的标记。预期的标记是 '"' 或 '''。第 1 行,位置 55
- listview - 有没有办法在颤动的 LIstView.Builder 中将 Epoch 时间戳格式化为人类日期格式?
- c++ - 使用 CMake 为 C++ 构建协议
- ruby-on-rails - 捆绑安装在我的项目 ruby on rails 中不起作用
- .htaccess - .htaccess 难以根据最后一页反斜杠“/”确定要使用的文件
- php - 带着laravel护照大摇大摆不工作
- search - 如果我在具有逗号分隔数字的文本字段上使用通配符搜索,Alfresco SOLR4 不会给出结果
- javascript - 使用 Javascript 生成和读取受密码保护的 ZIP