Selenium Fluent wait不进行任何轮询

Selenium Fluent wait不进行任何轮询,selenium,selenium-webdriver,wait,fluent,Selenium,Selenium Webdriver,Wait,Fluent,我遇到了一个关于SO的答案,其中两种流畅等待的方法是共享的,其中只有一种是做投票的,另一种是不做投票的 第一名: List<WebElement> eList = null; Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(30)) .pollingEvery(Duration.ofSe

我遇到了一个关于SO的答案,其中两种流畅等待的方法是共享的,其中只有一种是做投票的,另一种是不做投票的

第一名:

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)));
                }
            });
List-eList=null;
Wait Wait=new FluentWait(驱动程序)。带有超时(持续时间为秒(30))
.pollingEvery(持续时间为秒(5)).ignoreing(NoSuchElementException.class);
eList=(列表)wait.until(新函数(){
公共列表应用(WebDriver){
返回driver.findElements(By.xpath(xpathExpression));
}
});
秒:

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)wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(xpathExpression));
第二个1进行轮询,第一个不进行轮询。还可以在控制台中打印它以5秒的间隔尝试了30秒

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

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

    没有偏好,我只希望,因为它是流畅的,它应该这样做


    我做错了什么?

    在第一个场景中,轮询没有发生,因为您返回的是非空值,这是因为
    1.方法返回所有WebElements的列表,如果没有匹配项,则返回空列表,并且
    2.方法将等待,直到条件计算为既不为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;
                    }
                }
            });
    
    eList=(列表)等待.until(新函数(){
    公共列表应用(WebDriver){
    List=driver.findElements(By.xpath(xpathExpression));
    if(list.size()==0){
    返回null;
    }否则{
    退货清单;
    }
    }
    });
    

    在第二个scnario中:用于ExpectedConditions.presenceOfAllElementsLocatedBy(通过定位器),用于检查网页上是否至少存在一个元素的期望。

    一旦返回
    null
    它为什么会继续轮询?这不是一个循环。