What are the types of wait commands in Selenium Webdriver?

Overview

If we want to master using Selenium webdriver, one of the most important skills to learn is using the wait commands. They are essential for executing test scripts and help identify and resolve issues related to the time lag in web elements.

When a web page loads, different web elements (buttons, links, images) that someone wants to interact with may load at different intervals.

Selenium webdriver offers the following three commands to implement waits in tests.

  • Implicit wait
  • Explicit wait
  • Fluent wait

Implicit wait

Implicit wait directs the selenium webdriver to wait for a particular time before throwing an exception. Once this time is set, webdriver will wait for the element before the exception occurs.

Importing

We must import the following package to add implicit wait in our script.

import java.util.concurrent.TimeUnit;

Syntax

The following code sets an implicit wait in our test script.

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

Code

Following is an example to show how we can use implicit wait.

public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.google.com";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

Explanation

  • Line 1: We create a function to implement implicit wait.
  • Line 2: We initialize driver.
  • Line 3: This is the URL of the webpage on which we want to implement implicit wait.
  • Line 4: We set an implicit wait of 30 seconds.

Explicit wait

Using the explicit wait statement, we can instruct webdriver to wait until a particular condition occurs before continuing to execute code.

If we set the default wait command, the browser will wait the same time before loading each web element. Implicit wait causes an unnecessary delay in the execution of the test script.

The explicit wait can only use explicit wait for specified elements. However, it is an improvement on the implicit wait because it allows the program to pause for dynamically loaded ajax elements.

Importing

We must import the following package to add explicit wait in our script.

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

Syntax

The following code sets an explicit wait in our test script.

WebDriverWait wait = new WebDriverWait(driver,30);

Code

The following example shows how we can use an explicit wait.

public void setup() throws InterruptedException {
driver=new FirefoxDriver();
driver.get("https://gmail.com");
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));
}

Explanation

  • Line 1: Create a function to implement implicit wait.
  • Line 2: Initialization of the driver element.
  • Line 3: Open the webpage we want to implement explicit wait.
  • Line 4: Initialization of the wait element.
  • Line 5: Set explicit wait of visibilityOfElementLocated with a timeout of 30 seconds.

Fluent wait

Fluent wait in Selenium indicates that the maximum time Selenium webdriver has to wait before a particular condition (web element) becomes visible. It also defines how often the webdriver will check to see if the condition occurs before throwing an error, Elementnotvisibleexception.

Simply put, fluent wait repeatedly searches for a web element at regular intervals until a timeout occurs or until the object is found.

The fluent wait commands are most valuable when interacting with web elements that may take longer to load. This happens a lot in ajax applications.

Code

The following example shows how we can use a fluent wait.

FluentWait wait = new FluentWait(driver);
wait.withTimeout(5000, TimeUnit.MILLISECONDS);
wait.pollingEvery(250, TimeUnit.MILLISECONDS);
wait.ignoring(NoSuchElementException.class)
wait.until(ExpectedConditions.alertIsPresent());

Explanation

  • Line 1: We declare and initialize a fluent wait.
  • Line 2: We specify the timeout of the wait to 5000 milliseconds (5 seconds).
  • Line 3: We specify the polling time as 250 milliseconds.
  • Line 4: We specify what exceptions to ignore.
  • Line 5: We specify the condition to wait on.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved