Implicitly Wait in Selenium

Nowadays, most of the applications are formed using ajax statements, ajax makes the DOM elements to load at different times but selenium tries to find the element after the moment, it completes the previous line execution.

Due following reasons application element may take a while to load.

  • Poor Server Response time
  • Ajax Loading
  • Size of the page
  • Internet Speed
  • System hardware
  • Browser slowness / less responsiveness
  • Element created using javascript

Selenium tries to find the element without bothering about whether elements are loaded or not, and selenium throws NoSuchElementException if the element is not present.

Implicitly wait is one of the ways to request selenium not throw any exception until provided time. The default wait time of the selenium is 500 milliseconds, implicitly wait overrides the default wait time of the selenium.

If the element is found before implicitly wait time, selenium moves to the next commands in the program without waiting to complete the implicitly wait time; this wait is also called dynamic wait.

The implicit wait is set for the entire duration of your webdriver and is set at the start of your program. Most of the automation tester writes the implicit wait after the creation of browser object.

Implicit wait tries to find the element in the first go if the element is not present implicit wait tries to find the element after 500ms of first polling, if the element is not available on the second time also then implicit wait tries the third time after 500 ms of the second try and it goes on till the time reaches the 30 seconds.

What it does is, incase while executing, if your webdriver doesn't find any element then instead of throwing an exception, the implicit wait makes your driver to wait for the specified wait time and then try to find the element once again.

If the driver still does not find the element, then it throws an exception. Implicit wait does the same for all the elements in your program, so you just have to set it once.

WebDriver driver = new ChromeDriver();
// set implicit wait tme as 30 Seconds
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

Ex: Let's say that you give an Implicit wait of 30 sec, now while executing, your webdriver is not able to find an element, but since you've set implicit wait, the webdriver waits instead of throwing an exception.

Let's say the time the webdriver searched for the element is t=0. Now the webdriver waits for 30 sec, and at t=30.500( polling time of the implicitly wait is 500 milliseconds) driver tries to find the element again, if it finds the element, then it performs the particular action on the element, and if it doesn't, it throws NoSuchElementException.

Complete Implicitly wait program

public class MultiValueDropdownSelectByValue {
	public static void main(String[] args) throws Exception {
		// set the geckodriver.exe property
		System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
		// open firefox
		WebDriver driver = new FirefoxDriver();
		// set implicitly wait time for 1 Minute
		driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
		// open webpage
		// find the dropdown using xpath
		WebElement dropdownElement = driver.findElement(By.xpath("//select[@id='second']"));
		// create object for Select class
		Select dropdown = new Select(dropdownElement);
		// select options from the dropdown options which have value as 'donut', 'burger'

Handle dropdowns in selenium

Scope of Implicitly Wait in Selenium

Implicitly Wait is applicable only to findElement and findElements no other statement in selenium.

Once set, the implicit wait is set for the life of the WebDriver object instance. You can re-assign the implicit wait time anywhere you want.

When does Implicitly Wait is applicable for the findElements method. :

Implicit wait in selenium is applicable for all findElement statements, but the applicability of implicit wait to the findElements is Limited.

Implicitly wait is applicable on findElements only when there are no elements present, selenium webdriver moves to next command the moment it finds a single element.

If there is Zero matching elements selenium waits till the time it reaches provided implicitly wait.

Scenario 1 : Consider a page having 100 checkboxes and applied implicit wait is 30 Seconds
When the page is loading selenium tries to find all the matching elements, but at there moment there no matching elements.

After 10 seconds of wait 1 checkbox is loaded, now selenium finds that one checkbox and concludes that there is only one element, so it ends the wait and moves to the next command.

Selenium does not wait till 100 checkboxes loaded into the page, as the webdriver does not know how many elements going to be present on the page.

Scenario 2 : Consider a page having 0 checkboxes and applied implicit wait is 30 Seconds
Selenium tries to find all the matching elements in DOM, but so far no checkbox is loaded.

Now there are zero matches, so selenium waits till 30 seconds, and if does not find element after 30 seconds selenium throws NoSuchElementEXception

Exception in selenium

Supported Timeunits with Implicit Wait

Implicitly wait in selenium supports all the time unit from nano-Seconds to Days, all the units are present in the TimeUnit abstract class.

All the values (variable ) present in the TimeUnit are Constants, I guess you already got that from the UPPERCASE style itself. UPPERCASE is used to mean value is constant

  • TimeUnit.NANOSECONDS : represents nano seconds (10pow-9 of a second is nanosecond)
  • TimeUnit.MICROSECONDS : represents microseconds (10pow-6 of a second is microsecond)
  • TimeUnit.MILLISECONDS : represents mill seconds (10pow-3 of a second is microsecond, i.e. 1000 milliseconds is a second)
  • TimeUnit.SECONDS : represent a second
  • TimeUnit.MINUTES : represent minutes, a minute is 60 seconds
  • TimeUnit.HOURS : represent the hour, an hour is 60 minutes
  • TimeUnit.DAYS : represents the day, a day is 24 hours

Timeouts in Selenium

Implement ImplicitWait using sleep method in selenium

We can implement Implicitly Wait using Thread.sleep() method, by dividing the total wait time with 300ms time and we have to store that value in a variable for looping purpose, then we have to make the thread to sleep for the 300ms and then re-try to find the element. Follow the below steps to implement.

Explanation : Let's understand the principle of the implicitly wait, implicitlyWait waits for the element to present on a webpage with a given time limit, implicitly wait keeps trying to find the element till the time expires.

Steps to Implement :

  • Create a class called ImplicitWaitWithSleep.
  • Create a static method called implicitlyWithSleep, and the method should accept seconds as a parameter.
  • Initiator the count variable for counting purposes.
  • For calculation purposes, we have to convert seconds into milliseconds, as implicit wait polls once in 300 milliseconds.
  • Create a while loop with the condition as true; this is also called an infinite loop as we do not have any condition
  • Write our finding element code inside the try block, so that we can catch exceptions if the element is not there, make sure what kind of exception you want to catch ( for the implicit wait we have to catch NoSuchElementException ).
  • We have to break the loop when we find the element (inside try block).
  • Verify the condition that our tries( count ) exceeded the max time limit if it crosses throw a new NoSuchElementException exception.
  • We have to make the program to sleep for 300 milliseconds before polling again. With this end, the implicitlyWithSleep method.
  • Open and navigate to google page in the browser inside the main method.
  • Access the method to set the text to the google search bar
public class ImplicitWaitWithSleep {
	public static WebDriver driver;
	public static void main(String[] args) {
		// set chrome driver exe path
		System.setProperty("", "C:/~/chromedriver.exe");
		// create object for chrome browser
		 driver = new ChromeDriver();
		// navigate to google

	public static void implicitlyWithSleep(long timeLimit){
		System.out.println("Custom implicitly wait with selenium");
		//set the counter for terminate purpose
		long count = 0;
		// calculate the number of tries
		long tries = (timeLimit * 1000) /300;
			try {
				System.out.println("Trying for : "+count+" time(s)");
				// try to find the element and send keys
				driver.findElement(By.xpath("//input[@name='q']")).sendKeys("Implicit wait with sleep");
				System.out.println("element found");
				// end the loop as we found the element
			} catch (NoSuchElementException e) {
				if (count > tries) {
					// throw exception if element not found after exception.
					throw new NoSuchElementException("Element not fount after : "+timeLimit+"seconds");
				// sleep for 300 milli seconds,
				// try to find the element after 30 seconds of sleep
				try {
				} catch (InterruptedException e1) {
					System.out.println("Sleep interrupted");

Parameterized Constructor in Selenium and Java

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions