Mate Mrše
Mate Mrše

Reputation: 8394

WebDriverWait is deprecated in Selenium 4

I'm getting a

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

in Selenium 4.0.0-alpha-3.

But official Selenium page lists only

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

as deprecated.

What is wrong? I'm using IntelliJ, could it be their issue?

Upvotes: 19

Views: 108354

Answers (8)

Anj Raju
Anj Raju

Reputation: 686

Instead of the following lines of code:

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

Use this:

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

Basically, you are replacing the parameter and replacing two parameters (30, TimeUnit.SECONDS) with one (Duration.ofSeconds(30))

Upvotes: 0

Menaga Kumar
Menaga Kumar

Reputation: 1

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

Here which driver have to use for selenium 4? Earlier in selenium 3.141 version we have used EventfiringWebDriver

Upvotes: 0

Geetu Dhillon
Geetu Dhillon

Reputation: 121

Code which gives the below warning:

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

Warning:

The method implicitlyWait(long, TimeUnit) from the type WebDriver.Timeouts is deprecated.

Update which works on Selenium 4:

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

Upvotes: 12

Sameera De Silva
Sameera De Silva

Reputation: 1980

Write it like this with Selenium 4 since what you tried to use is deprecated, as you said.

First import.

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

For fluent wait.

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

WebDriverWait statement

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

Upvotes: 23

PDHide
PDHide

Reputation: 19929

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

Use this instead. Only WebDriverWait(driver, clock) is supported;

Upvotes: 8

undetected Selenium
undetected Selenium

Reputation: 193058

This warning message...

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

...implies that the current constructor of WebDriverWait have been deprecated.


Looking in to the code for WebDriverWait.java it seems:

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

Hence you see the error.


However, I don't see any changes to WebDriverWait Class in Seleniumv4.0.0-alpha* Java client changelog and the functionality should continue to function as per the current implementation.

Selenium Java client v4.0.0-alpha-3 changelog:

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.

Conclusion

Selenium's Java client v4.0.0-alpha-3 is still a alpha release and needs to go through beta release and hence shouldn't be used for testing activity in production environment.


Solution

An immediate solution would be to downgrade to current released level Version 3.141.59.

Upvotes: 3

Rajiv Agarwal
Rajiv Agarwal

Reputation: 49

This code snippet is working with Selenium 4.0:

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

Upvotes: 1

Guy
Guy

Reputation: 50809

It doesn't appear in the docs, but if you look at the source code you will see @Deprecated annotation

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

In the constructor description you have the solution

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

Which is the constructor being called from the deprecated one in any case.

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

Upvotes: 31

Related Questions