Explicit Wait in Selenium WebDriver

Here, we will discuss about Explicit Wait.

In previous Posts, we have seen behavior of Thread.sleep() Wait and Implicit Wait in Selenium WebDriver. Kindly read those Posts as it would help you to better understand about Explicit Wait.

What is Explicit Wait ?

Explicit Wait tells WebDriver to wait for a certain condition to occur before proceeding further in the code.

Explicit Wait provides better control when compare it with Implicit Wait. Unlike Implicit wait, Explicit Wait gives us flexibility to wait until specific condition is met for specific timeout period. If condition is met within the specific timeout period, then code will proceed further, otherwise WebDriver will throw an exception.

Explicit wait is commonly used when we need to wait for specific element or content after performing any action, like when application gives AJAX Call to the server in order to get the dynamic data and render it on specific section of a Web Page.

Let have a look at some common situations which usually occur while automating scenario :

a) Let say, there is only one drop down called ‘State’ in a web page. As soon as, user selects any particular state, then another drop down called ‘City’ will get visible in a page and options of ‘City’ drop down will get change depends on the state Selected.
Implicit Wait won’t work here as after selecting the state, the application gives AJAX call to server and only partial section of a page is updated. Implicit won’t recognize, if some part of a page got updated due to AJAX call.
Here, we have to use Explicit Wait which tells WebDriver to wait for certain condition to occur like we will write an explicit wait statement to wait until ‘City’ Drop down is visible for the specific timeout period.

b) In the second situation, there is one registration page where Submit button is initially disable . As soon as, user inputs all the required information, Submit button will get enable automatically. There might me case when Submit button takes some time to get enable.
If we set an Implicit wait in a script, then it will find the submit button immediately as it is available in DOM and perform a click on it and will fail because element is not yet enable.
In order to overcome from this situation, we have to use Explicit Wait statement to wait until Submit button is enable for the specific timeout period.

WebDriver provides WebDriverWait and ExpectedCondition classes for implementing an Explicit Wait.

Let’s have a look at some frequently used methods provided by ExpectedCondition class :

1. presenceOfElementLocated(By locator) : waits for an element until it is available in DOM for specific timeout period.


WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(locator));a]

In above code, 2 parameters are sent while initializing WebDriverWait Object
First Parameter : Driver Object
Second Parameter : Time out period in Seconds

2. visibilityOfElementLocated(locator) : waits for an element until it is visible on a web page for specific timeout period.


WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(locator));

3. elementToBeClickable(By locator) : waits for an element until it is clickable for specific timeout period.


WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(locator));

If you really like the information provided above, please don’t forget to like us on Facebook, you can also leave the comment.

Leave a Reply

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


Notice: Undefined offset: 0 in G:\PleskVhosts\automate-apps.com\httpdocs\wp-content\plugins\cardoza-facebook-like-box\cardoza_facebook_like_box.php on line 924