Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在FluentWait使用中使用lambda函数与不使用它有什么区别?_Java_Selenium_Selenium Webdriver_Webdriverwait_Fluentwait - Fatal编程技术网

Java 在FluentWait使用中使用lambda函数与不使用它有什么区别?

Java 在FluentWait使用中使用lambda函数与不使用它有什么区别?,java,selenium,selenium-webdriver,webdriverwait,fluentwait,Java,Selenium,Selenium Webdriver,Webdriverwait,Fluentwait,等待元素可以编码为 WebElement foo = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("foo"))); 在FluentWait的文档中,下面给出的示例排除了超时、轮询间隔和异常忽略的定义 WebElement foo = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDrive

等待元素可以编码为

WebElement foo = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("foo")));
在FluentWait的文档中,下面给出的示例排除了超时、轮询间隔和异常忽略的定义

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
 public WebElement apply(WebDriver driver) {
   return driver.findElement(By.id("foo"));
 }
});
WebElement foo=wait.until(新函数(){
公共WebElement应用(WebDriver){
返回驱动程序findElement(By.id(“foo”);
}
});
这两者的区别是什么?还有什么好处吗

我搜索了lambda表达式,函数接口。但是我没有完全了解情况。

WebDriverWait 是使用WebDriver实例的FluentWait的专门化

施工人员为:

  • WebDriverWait(WebDriver-driver,java.time.Clock-Clock,sleep-sleep,long-timeoutseconds,long-sleepTimeOut)
  • WebDriverWait(WebDriver-driver,long-timeoutingseconds)
    :诱导此等待将忽略默认情况下在“直到”条件下遇到(抛出)的实例,并立即传播所有其他实例
  • WebDriverWait(WebDriver-driver,long-timeoutingseconds,long-sleepinmilis)
    :诱导此等待将忽略默认情况下在“until”条件下遇到(抛出)的实例,并立即传播所有其他实例

WebDriverWait的Lambda实现 示例A:

(new WebDriverWait(driver(), 5))
    .until(new ExpectedCondition<WebElement>() {
        public WebElement apply(WebDriver d) {
            return d.findElement(By.linkText(""));
        }
    });
Exmaple C:

(new WebDriverWait(driver(), 5)).until((driver) -> driver.findElement(By.linkText("Google")));

FluentWait 接口的实现,可以动态配置其超时和轮询间隔

每个FluentWait实例定义等待条件的最长时间,以及检查条件的频率。此外,用户可以将等待配置为在等待时忽略特定类型的异常,例如在页面上搜索元素时

示例用法:

// Waiting 30 seconds for an element to be present on the page, checking for its presence once every 500 milliseconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(Duration.ofSeconds(30))
    .pollingEvery(Duration.ofMillis(500))
    .ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
    public WebElement apply(WebDriver driver) {
        return driver.findElement(By.name("q"));
    }
});
//等待30秒,让元素出现在页面上,每500毫秒检查一次元素的存在。
等待等待=新建FluentWait(驱动程序)
.withTimeout(持续时间秒(30))
.pollingEvery(持续时间:百万(500))
.忽略(NoSuchElementException.class);
WebElement foo=wait.until(新函数(){
公共WebElement应用(WebDriver){
返回驱动程序findElement(按名称(“q”);
}
});
注意:此类不提供线程安全保证

您可以在讨论中找到FluentWait的工作示例

// Waiting 30 seconds for an element to be present on the page, checking for its presence once every 500 milliseconds.
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(Duration.ofSeconds(30))
    .pollingEvery(Duration.ofMillis(500))
    .ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
    public WebElement apply(WebDriver driver) {
        return driver.findElement(By.name("q"));
    }
});