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 -


Implicit waits in selenium 4

Same with the other waits like scriptTimeout and pageLoadTimeout :-


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

After Selenium 4 -

//Selenium 4 syntax
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(10));

FluentWait in Selenium 4 -

Before Selenium 4 -

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

After Selenium 4 -

    Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)

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: 


Thanks for sharing detailed wait statements

Add new comment

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