如何在Selenium Java Client v3.11.0中删除超时和轮询时的弃用警告

kri*_*das 7 java selenium webdriver selenium-webdriver fluentwait

下面是我的代码,在我将Selenium Webdriver版本更新为3.11.0之后,该代码显示为已弃用.

    private Wait<WebDriver> mFluentWait(WebDriver pDriver) {
    Wait<WebDriver> gWait = new FluentWait<WebDriver>(pDriver).withTimeout(100, TimeUnit.SECONDS)
            .pollingEvery(600, TimeUnit.MILLISECONDS).ignoring(NoSuchElementException.class);   
    return gWait;
}
Run Code Online (Sandbox Code Playgroud)

在代码中显示已弃用的警告withTimeoutpollingEvery部分.

如何重写此代码以便我可以删除已弃用的警告.

由于我是硒的新手,我不确定这种变化.任何帮助将不胜感激.

Deb*_*anB 13

@Grasshopper的回答指出了FluentWait的确切修改构造函数以及从withTimeoutpollingEvery字段中删除弃用警告的要求.如果你面临更大的困难,你可以使用下面的代码行:

import java.time.Duration;
//lines of code
Wait<WebDriver> gWait = new FluentWait<WebDriver>(pDriver).withTimeout(Duration.ofSeconds(100))
        .pollingEvery(Duration.ofMillis(600)).ignoring(NoSuchElementException.class);
Run Code Online (Sandbox Code Playgroud)

您可以在FluentWait类型中找到详细的讨论.它不能通过Selenium和Java为FluentWait类的参数设置参数化


小智 6

您可以使用以下代码行:

  Wait<Browser> wait = new FluentWait<>(driver)
            .withTimeout(Duration.ofSeconds(*timeToWaitInSec*))
            .pollingEvery(Duration.ofMillis(*TimeToTryinMillisec*))
            .ignoring(WebDriverException.class);
Run Code Online (Sandbox Code Playgroud)


Gra*_*per 5

检查FluentWait的源代码,提及使用Duration作为参数的方法.

  1. withTimeout - 使用withTimeout(Duration duration)方法.
  2. pollingEvery - 使用该pollingEvery(Duration duration)方法.