WebDriverWait 在 Selenium 4 中被弃用

WebDriverWait is deprecated in Selenium 4

我要

Warning: (143,13) 'WebDriverWait(org.openqa.selenium.WebDriver, long)' is deprecated

在 Selenium 4.0.0-alpha-3 中。

但官方Selenium page仅列出

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

已弃用。

怎么了?我正在使用 IntelliJ,会不会是他们的问题?

这条警告消息...

Warning: (143,13) 'WebDriverWait(org.openqa.selenium.WebDriver, long)' is deprecated

...表示 WebDriverWait 的当前构造函数已被弃用。


查看 WebDriverWait.java 的代码似乎:

  • 以下方法已弃用

    • 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);
      }
      
  • 同时添加了以下方法

    • 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;
      }
      

因此您会看到错误。


但是,我没有看到 WebDriverWait Class in v4.0.0-alpha* Java 客户端更新日志有任何变化,功能应该会继续运行根据当前的实施。

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 版本,因此不应在生产环境中用于测试 activity。


解决方案

立即解决方案是降级到当前 发布级别 Version 3.141.59

它没有出现在文档中,但是如果你查看 source code 你会看到 @Deprecated 注释

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

构造函数描述中有解决方案

@deprecated Instead, use {@link WebDriverWait#WebDriverWait(WebDriver, Duration)}.

在任何情况下,哪个构造函数是从已弃用的构造函数中调用的。

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

改用这个,只支持WebDriverWait(driver, clock);

发出以下警告的代码:

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

警告:
类型 WebDriver.Timeouts 中的方法 implicitlyWait(long, TimeUnit) 已弃用。

适用于 selenium4 的更新:

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

此代码片段适用于 Selenium 4.0:

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

用 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));