Waits in selenium

Implicit Waits :
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.  Once set, the implicit wait is set for the life of the WebDriver object instance.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement myDynamicElement = driver.findElement(By.linkText("Gmail"));

Explicit Waits

Explicit waits are used to halt the execution till the time a particular condition is met or the maximum time has elapsed. Unlike Implicit waits, Explicit waits are applied for a particular instance only.WebDriver introduces classes like WebDriverWait and ExpectedConditions to enforce Explicit waits into the test scripts.

Object Instantiation for WebDriverWait class

WebDriverWait wait = new WebDriverWait(driver,30);

We create a reference variable “wait” for WebDriverWait class and instantiate it using WebDriver instance and maximum wait time for the execution to layoff. The maximum wait time quoted is measured in “seconds”.

Expected Condition

The above command waits for a stipulated amount of time or an expected condition to occur whichever occurs or elapses first.

Types of Expected Conditions

ExpectedConditions class provides a great help to deal with scenarios where we have to ascertain for a condition to occur before executing the actual test step.

ExpectedConditions class comes with a wide range of expected conditions that can be accessed with the help of the WebDriverWait reference variable and until() method.

Let us discuss a few of them at length:

1) elementToBeClickable() – The expected condition waits for an element to be clickable i.e. it should be present/displayed/visible on the screen as well as enabled.

Sample Code

2) textToBePresentInElement() – The expected condition waits for an element having a certain string pattern.

Sample Code
wait.until(ExpectedConditions.textToBePresentInElement(By.xpath(“//div[@id= ‘forgotPass’”), “text to be found”));

3) alertIsPresent()- The expected condition waits for an alert box to appear.

Sample Code
wait.until(ExpectedConditions.alertIsPresent()) !=null);

4) titleIs() – The expected condition waits for a page with a specific title.

Sample Code

5) frameToBeAvailableAndSwitchToIt() – The expected condition waits for a frame to be available and then as soon as the frame is available, the control switches to it automatically.

Sample Code

Example to understand implicit an explicit waits

Consider Below steps

  1. Launch the web browser and open the “gmail.com”
  2. Enter a valid username
  3. Enter a valid password
  4. Click on the sign in button
  5. Wait for Compose button to be visible after page load

lets automate above mentioned scenario using selenium.

import static org.junit.Assert.*;

import java.util.concurrent.TimeUnit;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

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

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

public class WaitDemo {

// created reference variable for WebDriver

WebDriver driver;


public void init() throws InterruptedException {

driver=new FirefoxDriver();



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



public void test() throws InterruptedException {



// explicit wait - to wait for the compose button to be click-able

WebDriverWait wait = new WebDriverWait(driver,30);
// click on the compose button as soon as the "compose" button is visible




public void end() {





Subscribe to YouTube

Training on Selenium

Like us on facebook



Cooking with Amol