Implicit Wait in Selenium WebDriver

In the Previous Post, we have seen a behavior of Thread.sleep() wait in Selenium WebDriver and its disadvantages. I would recommend you to go through with that tutorial first. Click Here.

Why Wait Concept is required in WebDriver ?

After reading the Previous Post, now we are clear that Wait concept is actually required in WebDriver Scripts to save them from fail due to some common application issues like Network Delays, Element loading, AJAX loading etc.

When you run WebDriver script without implementing the Wait Concept, WebDriver starts executing statements in a flow and it won’t wait for the web elements, if they are taking time to load on a web page and then it immediately throws following exception : NoSuchElementException.

In this tutorial, we will see the behavior of Implicit Wait provided by Selenium WebDriver.

Implicit Wait tells WebDriver to poll the DOM for certain amount of time when trying to find an element or elements, if they are not immediately present on a webpage and when the element is not present even after implicit wait expires then WebDriver will throw NoSuchElementException.
So basically, It actually tells selenium to wait for specific amount of time before throwing NoSuchElementException.

Implicit Wait is basically a dynamic wait as it actually speed up your script execution, if you compare it with a static wait : Thread.sleep()

Why Implicit Wait is called as Dynamic Wait ?

Let take an example, if implicit wait of 30 seconds in applied in a script and script finds an element in 5 seconds then the script won’t wait for another 25 seconds. Means, it moves ahead immediately as soon as it finds an element. And if element is not available in 30 seconds then WebDriver will throw an exception.

Command to set Implicit Wait


WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Implicit wait accept 2 parameters :

First Parameter will accept time as integer Value
Second Parameter will accept time measurements like SECONDS, MILISECOND, MINUTES, MICROSECONDS, NANOSECONDS, DAYS, HOURS, etc.

Implicit Wait can be set at the time of driver initialization. Once set, it will retain till the driver session exist. Mean to say, after setting an implicit wait, it will be applied on all elements of your test script until the driver session is ended using close() or quit() command.

Let’s understand it by following scenario :

1. Launch https://www.google.com in Chrome Browser
2. Type ‘Selenium’ in Search Box. [Suggestions related to search keyword might takes some to load]
3. Click on the Last suggestion

GoogleSearch

Sample Script


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestClass {

	public static void main(String s[]) {

		System.setProperty("webdriver.chrome.driver", "D:/workspace/TestProject/lib/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.MINUTES);

		driver.get("http://google.com");

		driver.findElement(By.name("q")).sendKeys("Selenium");

		driver.findElement(By.xpath("//div[@class='sbsb_a']//ul/li[4]")).click();

		driver.close();
	}


In the above script, we have set an implicit wait of 30 seconds. It means if elements is not located on web page within 30 seconds then WebDriver will thrown an Exception.

When you run above, it will execute successfully as we have applied an implicit wait of 30 seconds. If you won’t apply it in above code, it will fail right after typing the Search Keyword because Suggestions will take some time to load. This issue can be handled by setting up an implicit wait in your script.

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 *