WebDriverWait在Selenium 4中已被弃用。

17

我遇到一个警告:

警告:(143,13) 'WebDriverWait(org.openqa.selenium.WebDriver, long)'已被标记为过时

在Selenium 4.0.0-alpha-3中。

但是官方Selenium页面只列出了:

WebDriverWait(WebDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut)

已被弃用。

出了什么问题?我正在使用IntelliJ,可能是他们的问题吗?

8个回答

29

文档中没有提到,但是如果您查看源代码,您将看到@Deprecated注释。

@Deprecated
public WebDriverWait(WebDriver driver, long timeoutInSeconds) {
    this(driver, Duration.ofSeconds(timeoutInSeconds));
}

在构造函数描述中,您已经有了解决方案。

@deprecated 相反,请使用{@link WebDriverWait#WebDriverWait(WebDriver,Duration)}。

这是从任何情况下调用已弃用的构造函数的构造函数。

new WebDriverWait(driver, Duration.ofSeconds(10));

19

由于你尝试使用的方法已被弃用,所以请使用Selenium 4按照以下方法编写:

首先导入。

import java.time.Duration;

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(30));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(60));

用于流畅等待。

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

WebDriverWait语句

WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));

8
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

请使用以下替代方案。仅支持WebDriverWait(driver, clock);


1
请确保在使用 Selenium 4.0.0-alpha-7 版本时不要出现任何警告。 - CodeWorld

8

以下代码会引发警告:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

警告:

WebDriver.Timeouts 类中的 implicitlyWait(long, TimeUnit) 方法已被弃用。

适用于 Selenium 4 的更新:

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

3
这条警告消息...
Warning: (143,13) 'WebDriverWait(org.openqa.selenium.WebDriver, long)' is deprecated

这意味着WebDriverWait的当前构造函数已被弃用。


查看WebDriverWait.java的代码,似乎:

  • The following methods are deprecated:

  • public WebDriverWait(WebDriver driver, long timeoutInSeconds)

         @Deprecated
         public WebDriverWait(WebDriver driver, long timeoutInSeconds) {
           this(driver, Duration.ofSeconds(timeoutInSeconds));
         }
    
  • public WebDriverWait(WebDriver driver, long timeoutInSeconds, long sleepInMillis)

         @Deprecated
         public WebDriverWait(WebDriver driver, long timeoutInSeconds, long sleepInMillis) {
           this(driver, Duration.ofSeconds(timeoutInSeconds), Duration.ofMillis(sleepInMillis));
         }
    
  • public WebDriverWait(WebDriver driver, Clock clock, Sleeper sleeper, long timeoutInSeconds, long sleepInMillis)

         @Deprecated
         public WebDriverWait(
             WebDriver driver, Clock clock, Sleeper sleeper, long timeoutInSeconds, long sleepInMillis) {
           this(
               driver,
               Duration.ofSeconds(timeoutInSeconds),
               Duration.ofMillis(sleepInMillis),
               clock,
               sleeper);
         }
    
  • Whilst the following methods were added:

  • public WebDriverWait(WebDriver driver, Duration timeout)

         /**
          * @param driver The WebDriver instance to pass to the expected conditions
          * @param timeout The timeout when an expectation is called
          * @see WebDriverWait#ignoring(java.lang.Class)
          */
         public WebDriverWait(WebDriver driver, Duration timeout) {
           this(
               driver,
               timeout,
               Duration.ofMillis(DEFAULT_SLEEP_TIMEOUT),
               Clock.systemDefaultZone(),
               Sleeper.SYSTEM_SLEEPER);
         }
    
  • public WebDriverWait(WebDriver driver, Duration timeout, Duration sleep)

         /**
          * Wait will ignore instances of NotFoundException that are encountered (thrown) by default in
          * the 'until' condition, and immediately propagate all others.  You can add more to the ignore
          * list by calling ignoring(exceptions to add).
          *
          * @param driver The WebDriver instance to pass to the expected conditions
          * @param timeout The timeout in seconds when an expectation is called
          * @param sleep The duration in milliseconds to sleep between polls.
          * @see WebDriverWait#ignoring(java.lang.Class)
          */
         public WebDriverWait(WebDriver driver, Duration timeout, Duration sleep) {
           this(driver, timeout, sleep, Clock.systemDefaultZone(), Sleeper.SYSTEM_SLEEPER);
         }
    
  • WebDriver driver, Duration timeout, Duration sleep, Clock clock, Sleeper sleeper)

         /**
          * @param driver the WebDriver instance to pass to the expected conditions
          * @param clock used when measuring the timeout
          * @param sleeper used to make the current thread go to sleep
          * @param timeout the timeout when an expectation is called
          * @param sleep the timeout used whilst sleeping
          */
         public WebDriverWait(WebDriver driver, Duration timeout, Duration sleep, Clock clock, Sleeper sleeper) {
           super(driver, clock, sleeper);
           withTimeout(timeout);
           pollingEvery(sleep);
           ignoring(NotFoundException.class);
           this.driver = driver;
         }
    
因此你看到了错误。
然而,我没有在Selenium Java客户端v4.0.0-alpha的更改日志中看到WebDriverWait类的任何更改,其功能应该按照当前实现继续运行。

Selenium Java客户端v4.0.0-alpha-3变更日志:

v4.0.0-alpha-3
==============

* Add "relative" locators. The entry point is through the `RelativeLocator`.
  Usage is like `driver.findElements(withTagName("p").above(lowest));`
* Add chromedriver cast APIs to remote server (#7282)
* `By` is now serializable over JSON.
* Add ApplicationCache, Fetch, Network, Performance, Profiler,
  ResourceTiming, Security and Target CDP domains.
* Fixing Safari initialization code to be able to use Safari Technology
  Preview.
* Ensure that the protocol converter handles the new session responses
  properly.
* Expose devtools APIs from chromium derived drivers.
* Expose presence of devtools support on a role-based interface
* Move to new Grid, deleting the old standalone server and grid implementation.
* Switch to using `HttpHandler` where possible. This will impact projects that
  are extending Selenium Grid.
* Respect "webdriver.firefox.logfile" system property in legacy Firefox driver.
  Fixes #6649
* Back out OpenCensus support: OpenTracing and OpenCensus are merging, so
  settle on one for now.
* Only allow CORS when using a —allow-cors flag in the Grid server
* If you're using the Java Platform Module System, all modules
  associated with the project are generated as "open" modules. This
  will change in a future release.
* The version of Jetty being used is unshadowed.

结论

Selenium的Java客户端v4.0.0-alpha-3版本仍处于alpha发布阶段,需要经历beta发布阶段,因此不应在生产环境中用于测试活动。


解决方案

一个即时的解决方案是降级到当前发布版本 3.141.59版本


1
这段代码片段适用于 Selenium 4.0:
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

0
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));

在Selenium 4中应该使用哪个驱动程序?在Selenium 3.141版本中,我们使用了EventfiringWebDriver


0

不要使用以下代码行:

    driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

使用这个:
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));

基本上,您正在替换参数,并用一个参数(Duration.ofSeconds(30))替换两个参数(30, TimeUnit.SECONDS)。

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