首页 > 解决方案 > 等到元素可点击 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 帮助我继续推进我的项目。

标签: javascriptseleniumselenium-webdriverasync-await

解决方案


推荐阅读