Chromedriver 元素在点 (xxx, yyy) 不可点击。其他元素将收到点击:

Chromedriver Element is not clickable at point (xxx, yyy). Other element would receive the click:

我在将 Chromedriver 与 Selenium 结合使用时遇到以下可怕的错误:

"org.openqa.selenium.WebDriverException: Element is not clickable at point (xxx, yyy). Other element would receive the click: ..."

我知道这个已经讨论过了here

但是,如果我延迟大约 5 秒然后单击它就可以正常工作,那么我的情况在某种意义上有点不同。我不需要做任何特别的事情,只需等待。

我知道我可以使用 JS 或 Actions 强制点击,但我想要一种更优雅的处理方式,即仅在按钮变为可点击时点击按钮。问题是我不知道如何检查按钮是否可以点击。

我尝试了以下方法,但均无效:

1) ExpectedConditions.elementToBeClickable
2) ExpectedConditions.visibilityOf

有什么想法吗?

我可以想到两个你可以尝试的选项:

JavaScript

勾选描述文档加载状态的Document.readyState属性:

JavascriptExecutor jsExecutor = (JavascriptExecutor) input;
return jsExecutor.executeScript("return document.readyState;").equals("complete");

你可以等它变成"complete"。在很多情况下可能行不通,但值得一试。

循环

这可能是最简单的解决方案。不确定它的优雅,但应该在不浪费比需要更多时间的情况下完成工作(这只是一个非常基本的示例,不应按原样使用此代码):

while (true) {
    try {
        element.click();
    } catch (WebDriverException e) {
        // ignore
        Thread.sleep(millis);
    }

我使用自定义的 waitAndClick() 方法处理了这个问题,该方法使用递归如下:

int waitCounter = 0;

// Wait for an element to become clickable

public static void WaitAndClick(WebElement elementToBeClicked) throws InterruptedException, IOException {


    try
    {


        WebDriverWait wait = new WebDriverWait(driver, 20);
        WebDriverWait wait1 = new WebDriverWait(driver, 20);



        wait.until(ExpectedConditions.visibilityOf(elementToBeClicked));
                wait1.until(ExpectedConditions.elementToBeClickable(elementToBeClicked));

        elementToBeClicked.click();

    }

    catch(Exception  e)

    {
        MethodLibrary.Logger_Info("Element not clicked yet. waiting some more for " + elementToBeClicked);

        if(waitCounter <3){

            waitCounter++;

            WaitAndClick(elementToBeClicked);
        }

        waitCounter = 0;


    }



}