Waits and Timeout in Selenium 4

In Selenium 4, the parameters received in Waits and Timeout have changed from expecting (long time, TimeUnit unit) to expect (Duration duration) which you see a deprecation message for all our tests.

What are Wait commands in Selenium?

When executing selenium automation tests, we use waits to make our tests reliable and robust. Most commonly, while running automation tests, we see 'ElementNotVisibleException' if there is a delay in loading particular element which Webdriver wants to interact.

Waits and Timeout helps the user to overcome various issues while loading elements on a page after performing some action or navigating across different pages in the application.

Implicit Wait in Selenium 4

Let's see how to define implicit wait after upgrading to Selenium 4.

Before Selenium 4 -

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

Now we will see this as deprecated @Deprecated WebDriver.Timeouts implicitlyWait(long time, TimeUnit unit);

After Selenium 4 -

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

Implicit waits in selenium 4

Same with the other waits like scriptTimeout and pageLoadTimeout :-

driver.manage().timeouts().scriptTimeout(Duration.ofMinutes(2));
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(10));

Explicit Wait in Selenium 4

-

WebDriverWait is also now expecting a 'Duration' instead of a long for timeout in seconds and milliseconds.

The method is now deprecated in selenium public WebDriverWait(@NotNull org.openqa.selenium.WebDriver driver, long timeoutInSeconds)

Before Selenium 4 -

 //Old syntax
 WebDriverWait wait = new WebDriverWait(driver,10);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".classlocator")));

After Selenium 4 -

//Selenium 4 syntax
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".classlocator")));

FluentWait in Selenium 4 -

Before Selenium 4 -

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

After Selenium 4 -

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

Please do let us know if you face any issues upgrading to selenium 4 using comment form below. We will comment back to your query.

Selenium Tutorials: 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.