【问题标题】:Fluent wait doesnt do any polling流利的等待不做任何轮询
【发布时间】:2018-07-31 07:27:45
【问题描述】:

我在 SO 上遇到了一个答案,其中两种 Fluent 等待方法是共享的,只有一种轮询其他方法没有。

第一:

List<WebElement> eList = null;
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(30))
                .pollingEvery(Duration.ofSeconds(5)).ignoring(NoSuchElementException.class);

            eList  = (List<WebElement>) wait.until(new Function<WebDriver, List<WebElement>>() {
                public List<WebElement> apply(WebDriver driver) {
                    return driver.findElements(By.xpath(xpathExpression)));
                }
            });

第二:

只需在 #1 中更改以下内容

eList = (List<WebElement>) wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(xpathExpression)));

Second 1 轮询第一个没有。还在控制台中打印它尝试了 30 秒,间隔为 5 秒

我不担心为什么它不在控制台中打印,我的问题是

  1. 场景 2 - 在控制台中打印但未能找到元素,但元素始终存在。
  2. 场景 1 - 完全相反,即它在控制台中不打印任何关于轮询的内容,但能够立即找到元素,只有当元素已经存在于 DOM 中时。这意味着它在需要轮询时失败。

现在我最终都使用了,有时是 #1,有时是 #2。

我只期望没有偏好,因为它是流畅的等待它应该表现得像那样。

我做错了什么?

【问题讨论】:

    标签: selenium selenium-webdriver wait fluent


    【解决方案1】:

    在您的第一个场景中,没有发生轮询,因为您返回的是非空值,这是因为
    1. findElements() 方法返回所有 WebElement 的列表,如果没有匹配项,则返回一个空列表
    2. until() 方法将等待,直到条件评估为既不是 null 也不是 false 的值。

    因此,首先,您可以返回 null 以继续轮询,直到找到所需的元素(即当您获得列表的大小大于零时) 你可以试试下面的代码

    eList  = (List<WebElement>) wait.until(new Function<WebDriver, List<WebElement>>() {
                public List<WebElement> apply(WebDriver driver) {
                    List<WebElement> list=driver.findElements(By.xpath(xpathExpression));
                    if(list.size()==0){
                        return null;
                    }else{
                        return list;
                    }
                }
            });
    

    在第二个场景中:对于 ExpectedConditions.presenceOfAllElementsLocatedBy(By locator) ,检查网页上是否存在至少一个元素的期望。

    【讨论】:

    • 一旦返回null 为什么它会继续轮询?这不是 for 循环。
    猜你喜欢
    • 2016-02-15
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-04
    • 2012-03-27
    • 1970-01-01
    • 2010-10-30
    相关资源
    最近更新 更多