javascript - 等到元素可点击 Selenium Javascript 绑定
问题描述
我正在尝试单击一个按钮。现在我只是想点击它一次,我知道它会在那里。这个按钮被一些 JavaScript 加载了一堆其他的东西,因此需要一点时间才能被点击。截至目前,我正在尝试通过以下方式单击它:
const seeMoreBtn = await this.driver.findElement(showMoreBtnPath);
await waitUntilElementClickable(this.driver, seeMoreBtn, ONEMINUTE_TIMEOUT);
await seeMoreBtn.click();
我编写了 waitUntilElementClickable 函数,因为我不认为它是 JavaScript 绑定的一部分。定义如下:
async function waitUntilElementClickable(driver, element, timeout) {
await driver.wait(until.elementIsVisible(element), timeout);
await driver.wait(until.elementIsEnabled(element), timeout);
}
当我运行我的代码时,我收到了错误
(node:16076) UnhandledPromiseRejectionWarning: ElementClickInterceptedError: Element <button class="btn btn-default btn-sm ng-binding"> is not clickable at point (743,865) because another element <div class="clearfix animate-show ng-isolate-scope ng-hide-add ng-animate ng-hide-animate ng-hide ng-hide-add-active"> obscures it
由于页面上的信息,我无法附上屏幕截图,但驱动程序正在自动滚动到元素,并且没有等待 ONEMINUTE_TIMEOUT,所以我猜该元素是可见且已启用。
关于为什么此点击仍被拦截的任何提示将非常有帮助。谢谢!另外,我知道 waitUntilElementClickable 函数理论上可以将超时时间加倍,目前这没什么大不了的。
更新:@Prophet 在评论中建议的快速修复对我有用。我换了
await seeMoreBtn.click();
和
await this.driver.executeScript("arguments[0].click()", seeMoreBtn);
非常感谢@Prophet 帮助我继续推进我的项目。
解决方案
推荐阅读
- node.js - 节点获取函数不返回任何内容
- esp8266 - ESP8266 无法重新连接到上次保存的 WiFi 网络
- sql - 如何使用动态列标题联合所有表
- java - Spring Mvc - 创建自定义注释以从数据库中读取应用程序属性
- c# - 枚举部分的 DropDownListFor
- powershell - 在powershell中合并计数唯一路径
- r - 如何对 R Markdown 内联代码应用特殊格式?
- jquery - jQuery:TabAccordion - 如何找到 DOM 中的下一个元素单击此
- sql-server - Microsoft 记录的示例在 SQL Server 2017 中不起作用
- python - 在 python 中透视数据