如何使用lambda表达式替换WebDriverWait()?

3

我想了解如何使用lambda替换常见的WebDriverWait

WebDriverWait用于显式等待某些事件发生。

代码示例:

(new WebDriverWait(Driver.driver.get(), 10)).until(new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver d) {
        return d.findElement(By.id("DataTables_Table_0_processing")).isDisplayed();
    }
});

或者:

(new WebDriverWait(Driver.driver.get(), 10))
        .until(ExpectedConditions
                .invisibilityOfElementLocated(By.id("DataTables_Table_0_processing")));

如何使用Lambda表达式替换它?

如果您在IDE中进行此操作,每个体面的现代IDE都有一个快速修复或重构功能,例如“将匿名类转换为lambda表达式”。只需点击它,您就可以解决问题了。 - Tagir Valeev
1个回答

3
(new WebDriverWait(Driver.driver.get(), 10))
        .until(d -> d.findElement(By.id("DataTables_Table_0_processing")).isDisplayed());

我认为第二种情况不需要使用lambda表达式,因为已经有一个提供足够清晰度的便利方法。


在Selenium 4.2及以上版本中,我们应该使用什么来替代WebDriverWait? - Igor Kanshyn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接