November 21


1. Introduction of explicit wait in Selenium

2. Practical usage of explict wait

3. Source code

Introduction of explicit wait in Selenium

In the previous blog, we have studied about implicit wait, the default wait time selenium waits to find elements on web page. 

Sometimes we may want to wait extra time only for a certain element. For example, we might explicitly want to wait for 10 seconds before clicking a button, but we may set the implicit timeout of 5 seconds for all the other elements on the webpage.

Unlike implicit wait, the explicit wait is not a global timeout. Thus explicit wait is set for a specific object on the webpage. If there are 50 elements on a webpage and if a single element takes a long time to load, we can apply explicit wait for that specific element. 

Just for a single element, the implicit timeout (global timeout) should not be changed. 

Let us see how to setup explicit wait for a web element. 

Practical usage of explict wait

Let us go to 

The highlighted descrition (see above) says that when we press the blue colored button, the ajax text would appear after 15 secs.

Let us manually click the button, notice that the ajax data loads after 15 sec 

Inspect the blue colored button

We can make use of xpath to identify the button

Let us now inspect the AJAX data 

We can make use of xpath to identify the ajax output. 

Let us catch the text of ajax output in the string variable and try to print it

The code so far:

Execute test 

The test fails and ‘NoSuchElementException’ is thrown (see above). 

To resolve this exception, we will set the explicit wait timeout for only the above specific element. So, before fetching the innertext (line#21), we will explicitly wait for the ajax output 

Once the output is displayed on the webpage, the innertext will be fetched. 

Explicit wait can be implemented using inbuilt ‘WebDriverWait’ class.

Before line#23 (see below), we define ‘WebDriverWait’ class having reference ‘w’. This reference will wait upto 15 seconds maximum for an element, after that it will timeout

Now we can use ‘until’ method along with various conditions on this reference. As an example, visibilityOfElementLocated()

So, due to line#23, Selenium will explicitly wait for max 15 sec for the object to be visible

Once the element is visible, selenium will extract the text (line#25). Please note that the reference ‘w only knows about the element mentioned in line#25, the reference does not have any knowledge about other elements on the webpage

Execute the test

The ajax text gets printed in console

This is the magic of explicit wait. It waits only for specific webelement on the page upto the duration of explicit wait time mentioned in the script.

Let us comment line#21 and add line#22 to include 14 sec explicit wait time

The script should fail this time since the timeout is 14 sec while the Ajax data appears after 15 seconds.

Let’s see and execute the test. 

As expected, we get ‘TimeoutException’

Let us comment line#22 and add line#23 to include 16 sec explicit wait time

The script should pass this time since the explicit wait is greater than 15 secs.

Let’s see and execute the test. The script passes

This is how explicit wait works for a specific element.

Code snippet 

package Intro;

import java.time.Duration;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;



import io.github.bonigarcia.wdm.WebDriverManager;

public class Blog9_ExplicitWait {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();



//WebDriverWait w = new WebDriverWait(driver, Duration.ofSeconds(15)); //success

//WebDriverWait w = new WebDriverWait(driver, Duration.ofSeconds(14)); //fail

WebDriverWait w = new WebDriverWait(driver, Duration.ofSeconds(16)); //success


String ajax_message =  driver.findElement(By.xpath("//p[@class='bg-success']")).getText();





You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}