【问题标题】:Handle the NoSuchElementException in Fluent Wait在 Fluent Wait 中处理 NoSuchElementException
【发布时间】:2017-11-30 14:04:27
【问题描述】:

我知道,就等待不在DOM 中的网络元素而言,最有效的是流畅的等待。所以我的问题是:

有没有办法处理和捕获 NoSuchElementException 或由于元素不存在而导致流畅等待可能引发的任何异常?

我需要一个布尔方法,无论是否找到该元素,它都会给我结果。

这种方法在网络上很流行。

public void waitForElement(WebDriver driver, final By locator){
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(60, TimeUnit.SECONDS)
            .pollingEvery(2, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);

   wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    });
}

我需要的是,**.ignoring(NoSuchElementException.class);** 不会被忽略。一旦异常被捕获,它将返回 FALSE。另一方面,当找到一个元素时,它将返回 TRUE。

【问题讨论】:

  • 通过要求忽略.ignoring(NoSuchElementException.class);,您正在破坏FluentWait 的基本逻辑。我认为适合你的是WebDriverWait
  • 我可以使用 WebDriver 等待轮询吗?
  • 当然可以

标签: java selenium-webdriver selenium-chromedriver nosuchelementexception fluentwait


【解决方案1】:

作为替代方案,您希望看到 WebDriverWait 的轮询实现,这里是构造函数的详细信息:

  • WebDriverWait(WebDriver driver, long timeOutInSeconds):Wait 将忽略在“直到”条件下默认遇到(抛出)的 NotFoundException 实例,并立即传播所有其他实例。

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    
  • WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis):Wait 将忽略在“直到”条件下默认遇到(抛出)的 NotFoundException 实例,并立即传播所有其他实例。

    WebDriverWait wait2 = new WebDriverWait(driver, 10, 500);
    

更新:

要回复您的评论,您需要在此处定义 WebDriverWait 实例。接下来我们必须通过适当的ExpectedConditions 子句在您的代码中实现WebDriverWait 实例,即wait1 / wait2

【讨论】:

  • 非常感谢您对此的解释。但是,我必须捕获异常才能知道是否找不到元素。
  • 当然。看到我们刚刚在这里定义了WebDriverWait 实例。接下来我们必须通过ExpectedCondition 子句在我们的代码中实现它。请参阅此处的 ExpectedCondition 文档。
  • 知道了。 :) 我的答案如下。 :D 谢谢!也学到了一些东西。你的回答比它更有价值。 :) 其他读者也可以从中学习。
【解决方案2】:

这里是:

public boolean waitForElementBoolean(WebDriver driver, By object){
    try {
        WebDriverWait wait = new WebDriverWait(driver,60);
        wait.pollingEvery(2, TimeUnit.SECONDS);
        wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(object));
        return true;
    } catch (Exception e) {
        System.out.println(e.getMessage()); 
        return false;
    }
}

我将流利等待与显式等待集成在一起。 :D 谢谢你们! :)

【讨论】:

    【解决方案3】:

    您可以将WebDriverWaitpollingignoring 一起使用

    例子:

    public boolean isElementPresentWithWait(WebDriver driver, WebElement element) {
        try {
            WebDriverWait wait = new WebDriverWait(driver, 10);
            wait.pollingEvery(3, TimeUnit.SECONDS).ignoring(NoSuchElementException.class).until(ExpectedConditions.visibilityOf(element);
            return true;
        } catch (TimeoutException e) {
            return false;
        }
    }
    

    方法ignoringpollingEvery 返回FluentWait&lt;WebDriver&gt; 的实例

    【讨论】:

    • 问题是,我需要处理异常。所以忽略它是没有用的。 :|
    • 您可以通过if(isElementPresentWithWait(element)) {} else {}处理:)
    • 如果是分开的有区别吗? wait.pollingEvery(5, TimeUnit.SECONDS); wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(object));
    • 没有。它的工作方式相同。它被称为method chaining。您甚至可以使用以下new WebDriverWait(driver, 10).pollingEvery(3,TimeUnit.SECONDS) .ignoring(NoSuchElementException.class) .until(ExpectedConditions.visibilityOf(element); 编写 1 行代码
    【解决方案4】:

    你可以试试下面的代码 sn-p

        /*
     * wait until expected element is visible
     */
    public boolean waitForElement(WebDriver driver, By expectedElement) {
        boolean isFound = true;
        try {
            WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds , 300);
            wait.until(ExpectedConditions.visibilityOfElementLocated(expectedElement));
            makeWait(1);
        } catch (Exception e) {
            //System.out.println(e.getMessage());
            isFound = false;
        }
        return isFound;
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2013-12-15
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多