流利的等待不忽略异常

我正在使用流畅的等待,因此我可以在一段时间内忽略某些异常,尤其是ElementClickinterceptedException 这就是我宣布等待的方式:

private Wait<WebDriver> initiateWebDriverWait(int timeoutSeconds) {
        List allExceptions = new ArrayList();
        allExceptions.add(NoSuchElementException.class);
        allExceptions.add(ElementNotVisibleException.class);
        allExceptions.add(StaleElementReferenceException.class);
        allExceptions.add(ElementClickinterceptedException.class);

        return new FluentWait<>(driver)
                .withTimeout(Duration.ofSeconds(timeoutSeconds))
                .pollingEvery(Duration.ofMillis(100))
                .ignoreAll(allExceptions);
    }

用法:

    public void waitForElementThenClick(WebElement webElement,int timeOutSeconds) {
        waitForElementToBeclickable(webElement,timeOutSeconds);
        webElement.click();
    }


    public void waitForElementToBeclickable(WebElement webElement,int timeoutSeconds) {
        Wait<WebDriver> wait = initiateWebDriverWait(timeoutSeconds);
        wait.until(ExpectedConditions.elementToBeclickable(webElement));
    }

所以当我使用waitForElementThenClick时,我仍然得到

  

org.openqa.selenium.ElementClickinterceptedException:元素单击被拦截:元素...在点(1338,202)不可单击。其他元素将获得点击:

这是一个随机覆盖,仅在最短的时间内出现,我可以添加100ms等待等等,但是我的主要问题是为什么我什至看到这个异常,当我专门说它忽略它时至少持续5秒钟?而且它并没有等待这5秒钟,所以这不是超时。

有什么想法吗? 是webElement.click();抛出异常?如果是这样,为什么waitForElementToBeclickable返回true?

谢谢

qw25585613 回答:流利的等待不忽略异常

如果webdriver说该元素是可单击的,则并不总是意味着该元素确实是可单击的。可能是目标元素被另一个元素覆盖。 这是一个例子。打开https://stackoverflow.com/jobs?so_medium=StackOverflow&so_source=SiteNav。让我们尝试检查此元素是否可单击,然后单击它: enter image description here

但是,我们将此元素隐藏在下拉菜单中,如下所示: enter image description here

有xpath:By.xpath("//*[text()='Developer jobs']")

因此,如果我们检查此元素的条件ExpectedConditions.elementToBeClickable,它将返回true(就像元素可以单击一样)。但是,当您执行element.click m时,会发生ElementClickInterceptedException。

,

仅在until等待条件成立时才考虑例外列表。 click()在此之后发生,因此没有任何东西可以抓住ElementClickInterceptedException

elementToBeClickable检查元素是否可见,如果元素的甚至一部分可见,则为true,如果启用了元素,则除非元素包含{明确的true属性,但Selenium尝试单击可能覆盖的元素中间。要解决此问题,您可以等待叠加层消失,然后等待元素可点击

disabled
本文链接:https://www.f2er.com/3057706.html

大家都在问