WebDriverWait 在 Selenium 4 中已弃用

Posted

技术标签:

【中文标题】WebDriverWait 在 Selenium 4 中已弃用【英文标题】:WebDriverWait is deprecated in Selenium 4 【发布时间】:2020-03-18 11:48:30 【问题描述】:

我得到了一个

警告:(143,13) 'WebDriverWait(org.openqa.selenium.WebDriver, long)' 已弃用

在 Selenium 4.0.0-alpha-3 中。

但官方Selenium page只列出

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

已弃用。

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

【问题讨论】:

【参考方案1】:

此警告消息...

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;

因此您会看到错误。


但是,我在 Seleniumv4.0.0-alpha* Java 客户端更改日志中看不到对 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 版本,因此应该不能用于生产环境中的测试活动。


解决方案

一个直接的解决方案是降级到当前的发布级别 Version 3.141.59

【讨论】:

【参考方案2】:

它没有出现在文档中,但是如果您查看source code,您会看到@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));

【讨论】:

【参考方案3】:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

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

【讨论】:

在 selenium 版本 4.0.0-alpha-7 中使用后没有警告【参考方案4】:

给出以下警告的代码:

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

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

适用于 selenium4 的更新:

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

【讨论】:

【参考方案5】:

此代码 sn-p 与 Selenium 4.0 一起使用:

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

【讨论】:

【参考方案6】:

如您所说,使用 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));

【讨论】:

以上是关于WebDriverWait 在 Selenium 4 中已弃用的主要内容,如果未能解决你的问题,请参考以下文章

Selenium之WebDriverWait

Python Selenium.WebDriverWait 对Cookies的处理及应用『模拟登录』

Python Selenium.WebDriverWait 对Cookies的处理及应用『模拟登录』

Python-Selenium:Chrome 无头设置不适用于“WebDriverWait”

Selenium 高阶应用之WebDriverWait 和 expected_conditions

Python Selenium.WebDriverWait 判断元素是否存在