Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Chromedriver元素在点(xxx,yyy)处不可单击。其他元素将收到单击:_Java_Selenium_Selenium Chromedriver - Fatal编程技术网

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

Java Chromedriver元素在点(xxx,yyy)处不可单击。其他元素将收到单击:,java,selenium,selenium-chromedriver,Java,Selenium,Selenium Chromedriver,在将Chromedriver与Selenium一起使用时,我遇到了以下可怕的错误: "org.openqa.selenium.WebDriverException: Element is not clickable at point (xxx, yyy). Other element would receive the click: ..." 我知道这已经讨论过了 然而,我的情况是有点不同的意义上说,如果我把一个约5秒的延迟,然后点击它的工程罚款。我不需要做什么特别的事,就等着吧 我知道我可以

在将Chromedriver与Selenium一起使用时,我遇到了以下可怕的错误:

"org.openqa.selenium.WebDriverException: Element is not clickable at point (xxx, yyy). Other element would receive the click: ..."
我知道这已经讨论过了

然而,我的情况是有点不同的意义上说,如果我把一个约5秒的延迟,然后点击它的工程罚款。我不需要做什么特别的事,就等着吧

我知道我可以通过使用JS或操作来强制点击,但我想要一种更优雅的方式来处理这个问题,即只有当按钮变得可点击时才点击按钮。问题是我不知道如何检查按钮是否可点击

我尝试了以下方法,但都没有成功:

1) ExpectedConditions.elementToBeClickable
2) ExpectedConditions.visibilityOf

有什么想法吗?

我想有两种方法你可以试试:

JavaScript

检查描述文档加载状态的
Document.readyState
属性:

JavascriptExecutor jsExecutor = (JavascriptExecutor) input;
return jsExecutor.executeScript("return document.readyState;").equals("complete");
你可以等待它变得“完整”。在很多情况下可能不起作用,但值得一试

循环

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


我使用自定义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;


    }



}
//等待元素变为可单击
publicstaticvoidwaitandclick(webelementelementtobeclicked)抛出interruptedeexception,IOException{
尝试
{
WebDriverWait wait=新的WebDriverWait(驱动程序,20);
WebDriverWait wait1=新的WebDriverWait(驱动程序,20);
等待.直到(预期条件.可视性(ElementToBicked));
等待1.直到(ExpectedConditions.ElementToBickable(ElementToBicked));
要单击的元素。单击();
}
捕获(例外e)
{
MethodLibrary.Logger_Info(“尚未单击元素。正在等待更多元素以等待”+ElementToBicked);

如果(waitCounter我想我可以写一个方法,不断尝试点击,直到点击成功,即尝试/延迟捕获?这会被认为是一个优雅的解决方案吗?我讨厌把thread.sleeps放在任何地方。谢谢你的回复。循环选项是最后的选择,因为我认为它不优雅。我会测试JS版本并让你知道。@ratsstack好吧,请记住,许多“漂亮”的实现基本上只在内部执行相同的循环:)例如,看看Selenium
FluentWait
类中的
until
方法。这就是它的基本功能。抱歉,我不清楚“输入”是什么在这种情况下会是什么?@ratsstack这是你的
WebDriver
instance@ratsstack,而循环应该有一些超时,否则代码将处于无限循环中。
// 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;


    }



}