Selenium Wait Types

Sachin Joshi
3 min readMay 11, 2022

Every User Interface(UI) test case needs a wait to perform next set of instructions after perform some action in the UI.

As we perform one instruction there is something(this something depends on the action what you perform in the UI) which happens in the background which needs some time to give response back to browser and load the Document Object Model (DOM).

WebDriver does not track the active/real-time status of DOM and hense, we need waits.

In Selenium, we have 3 different types of waits.

  1. Implicit wait
  2. Explicit wait
  3. Fluent wait

Let’s learn them one-by-one.

1. Implicit wait

By Using Implicit wait, WebDriver polls the DOM for a specified duration when trying to find any element.

This wait is helpful when certain element is not available immediately on the webpage and require some time to load.

Implicit wait work on session basis and those are not enabled Default. someone who wants to use Implicit wait has to enable it manually.

Once you set the Implicit wait, it is available throughout the life of session unless you explicitly change it.

Example:

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

In above line, we have instructed webdriver to poll the DOM after 10 seconds.

2. Explicit wait

Explicit wait holds the program execution until the specified condition is occurred.

The condition specified in Explicit wait will be called with a certain frequency until the timeout of the wait is elapsed. It means, as long as the condition is not returning a true, it will keep trying and waiting.

It is suitable for synchronising between DOM & its browser eventually with your WebDriver script.

If condition is met, it will come out of wait and proceed with the execution. this helps in faster execution & saves time.

If condition is not met & timeout is elapsed. It will through TimeoutException

Example:

WebDriverWait wait = new WebDriverWait(driver,20);wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“//div[contains(text(),’Welcome’)]”)));

For above example, you need to use below 3 imports.

import org.openqa.selenium.By;import org.openqa.selenium.support.ui.WebDriverWait;import org.openqa.selenium.support.ui.ExpectedConditions;

As you have seen I have used ExpectedConditions in above example. you can use it for different scenarios as listed below( syntax may differ from lang to lang)

  • alert is present
  • element exists
  • element is visible
  • title contains
  • title is
  • element staleness
  • visible text
  • and much more …

3. Fluent wait

FluentWait(Smart waits) marks the maximum amount of time to wait for a condition and frequency by which the condition needs to be checked.

Fluent Wait searches for a web element at regular(specified) intervals until timeout or the object is found.

This is mostly helpful in applications developed in Ajax.

Exmaple:

FluentWait wait = new FluentWait(driver);wait.withTimeout(5, TimeUnit.SECONDS);wait.pollingEvery(250, TimeUnit.MILLISECONDS);

you need below import to work with FluentWait.

import org.openqa.selenium.support.ui.FluentWait;

you may also configure the wait to ignore specific types of exceptions like NoSuchElementException.

wait.ignoring(NoSuchElementException.class);

you need below import to work with FluentWait.

import org.openqa.selenium.NoSuchElementException;

Key Note:

Do not use implicit and explicit waits together. If you do so you may get some unpredictable wait times.

For example, if you set an implicit wait of 10 seconds along with explicit wait of 25 seconds it could cause a timeout to occur after 30 seconds.

Hope you have liked the topic !

Keep Reading, Keep Learning !!!

hit the follow button for more topics on Java, RestAssured, GraphQL, Selenium, JavaScript, JUnit, Jenkins, GitHub Actions, WebUI, SOAP & VBScripts

https://medium.com/@sachinjoshi12687

Also, DM me if you want me to add new topic of your choice urgently.

--

--

Sachin Joshi

follow me for more topics on Java, RestAssured, GraphQL, Selenium, JavaScript, JUnit, Jenkins, GitHub Actions, WebUI, SOAP & VBScripts