Hello Folks,

In last post, we have learnt about Thread.sleep() method. In this post we will learn:

  1. What is implicit wait?
  2. How to use implicit wait?
  3. What is behavior of implicit wait when element is found before specified time?
  4. What is behavior of implicit wait when element is not found and specified wait time expires?
  5. Difference between implicit wait and sleep methods?
  6. Disadvantages of implicit wait?

We have seen disadvantages of using sleep() method in last post. Selenium WebDriver provides another method called “Implicit Wait” to allow us to make WebDriver to wait. Let’s learn about it.

What is implicit wait?

  • Implicit wait specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
  • In last post’s example, we see that search results appear after some times. Search results are not available immediately. So, we put some sleep so that search results appear and we can locate and select required result from search results. The same we can achieve by setting implicit wait.
  • You must be thinking, sleep() and implictWait() sound same in behavior then what is difference? Why should we use implicitwait() over sleep()?
  • When we set implicit wait for some time say 30 seconds, driver will poll the page continuously(with gap of fraction of seconds between polls) until the element is found. Suppose, if element is found in 10 seconds only, WebDriver will not wait for remaining 20 seconds. It will go for the execution of next statements. It means implicit wait is a dynamic wait.
  • Implicit wait allow programmer to provide dynamic wait. It will speedup execution as well because using sleep() execution will be hold till the specified time is up.

Syntax of implicit wait:

WebDriver.Timeouts implicitlyWait(long time,java.util.concurrent.TimeUnit unit)

implicitWait methods takes two arguments:

time – The amount of time to wait.
unit – The unit of measure for time which could be SECONDS, MILLISECONDS, MINUTES etc.

To call this method, we need to use a method chain as below:

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

If you want to know about above method chaining, refer this post.

Java Code to understand above point:


import java.util.Date;
import java.util.concurrent.TimeUnit;

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

public class ImplicitWaitEx {

	
public static void main(String[] args) throws InterruptedException {
		
		System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
		// Opening a browser
		WebDriver driver= new ChromeDriver();
		driver.manage().window().maximize();
		
		// Loading a URL
		driver.get("https://www.redbus.in/");
		// Locating and typing in From text box. 
		WebElement fromTextBox= driver.findElement(By.id("src"));
		fromTextBox.sendKeys("Ban");
		// setting implicit time
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		System.out.println("Wait starts:"+new Date());
		System.out.println("Typed Ban");
		// Clicking on first search result
		driver.findElement(By.xpath("//li[@select-id='results[0]']")).click();
		System.out.println("Selected Bangalore");
		System.out.println("Wait ends:"+new Date());
		//Closing browser
		driver.quit();
		System.out.println("browser closed");
		
		
	}
}

Output:

Wait starts:Tue May 30 22:15:07 IST 2017
Typed Ban
Selected Bangalore
Wait ends:Tue May 30 22:15:08 IST 2017
browser closed

Above program proves:

  1. We are able to make WebDriver to wait using implicit wait.
  2. It is dynamic wait as WebDriver waited just for a second not for entire specified time. Many interviewer asked to prove this point. Just write above code and show it.

Behavior of Implicit wait when specified time expires:

  • What will happen if element is not found even after implicit wait time expires? Simple, it throw NoSuchElementException which is thrown when web element is not found. For example: Suppose we set implicit wait time as 30 seconds and element is not found even after 30 seconds , it will throw NoSuchElementException.

Java Program to prove above point:

We will try to locate a wrong web element purposely to achieve timeout.


package MakeSeleniumEasy;

import java.util.Date;
import java.util.concurrent.TimeUnit;

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

public class ImplicitWaitEx {

	
public static void main(String[] args) throws InterruptedException {
		
		System.setProperty("webdriver.chrome.driver", "./exefiles/chromedriver.exe");
		// Opening a browser
		WebDriver driver= new ChromeDriver();
		driver.manage().window().maximize();
		
		// Loading a URL
		driver.get("https://www.redbus.in/");
		// Locating and typing in From text box. 
		WebElement fromTextBox= driver.findElement(By.id("src"));
		fromTextBox.sendKeys("Ban");
		// setting implicit time
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		System.out.println("Wait starts:"+new Date());
		System.out.println("Typed Ban");
		// Clicking on first search result
		driver.findElement(By.xpath("//li[@select-id='results[100]']")).click();
		System.out.println("Selected Bangalore");
		System.out.println("Wait ends:"+new Date());
		//Closing browser
		driver.quit();
		System.out.println("browser closed");
		
		
	}
}

Output:

See the marked content. It shows duration of timeout. WebDriver waits for 30 seconds before it throws exception. It proves that WebDriver waits for maximum specified time in implicit wait if web element is not found.

Do we need to write multiple implicit wait statements like Thread.sleep()?

  • We have seen that you need to write Thread.sleep() wherever you want WebDriver to wait. It is not the case with Implicit wait. Once you set implicit wait, it will be applicable for all findElemnt() and findElements() method. There is no need to write implicit wait code again and again.
  • If we set implicit wait as 30 seconds, It will be applicable to all web element to be located by WebDriver.
  • When searching for a single element, the driver should poll the page until the element has been found, or this timeout expires before throwing a NoSuchElementException. When searching for multiple elements, the driver should poll the page until at least one element has been found or this timeout has expired.
  • If you write implicit wait statement multiple times, time specified in latest implicit wait statement will be applicable. For example: You specify implicit wait as 30 seconds at line no 5 and another implicit wait as 60 seconds at line no 10, then implicit wait as 30 seconds will be applicable from line no 6-9 and implicit wait for 60 seconds will be applicable from line no 11 onward.
  • So, if interviewer asks you how to change implicit time, you can answer easily.
  • Selenium WebDriver does not provide any direct getter method to retrieve implicit wait time. But you can create your own methods to retrieve it.
  • It is good practice to define implicit wait in the beginning of the program when we create WebDriver reference variable.
  • The default setting is Zero. Once set, the implicit wait is set for the life of the WebDriver object instance.

Difference between implicit wait and sleep methods:

  1. Implicit wait is dynamic while sleep is not.
  2. WebDriver will hold the execution of next statements till element is found while sleep methods makes WebDriver idle till the specified time expires.
  3. Once implicit wait is set it is for entire lifetime of WebDriver while you need to write sleep statement everytime wherever required.
  4. Implicit wait does not throw any checked exception while sleep does.
  5. Using sleep can increase test execution time drastically compare to implicit time.
  6. Syntax is different for both.

Disadvantages of using implicit wait in Selenium:

  • Increasing the implicit wait timeout should be used judiciously as it will have an adverse effect on test run time, especially when used with slower location strategies like XPath.
  • If you use the implicit wait in selenium it applies to the web driver globally and increases the execution time for the entire script. so it is not always advisable.
  • You can not wait till some specific condition is satisfied like invisibility of element, when alert is present etc.

That’s it guys. I tried to cover from basics to advanced concepts about implicit wait. If you have any doubts, feel free to comment.

Thank you.

#HappyLearning

13 thoughts on “Part 2: Waits in Selenium: Implicit Wait

  1. “If you use the implicit wait in selenium it applies to the web driver globally and increases the execution time for the entire script.”
    Why does this increase the execution time of the script?? I read that so often but never understood this part. This is a dynamic wait, means if the element could be found the script is going on with the next script command and it does not wait until the whole implicit waiting time is over. If the element cannot be found it does not make any sense to go on. So we need to wait until the element we want to be found is really found. Elements found earlier do not wait anymore.
    And if we (for whatever reason) need to use a slower location strategy like xpath, still we need to wait until the element with our slow xpath is found. Then not the implicit wait method is bad, the location strategy is. So why to be careful using implicit waits??

    1. Once you set implicit wait, it will be used whenever webdriver will try to find any webelement or webelements. Suppose you have a scenario where you need to check element should not be present. You will try to locate element and when it throws no such element exception the you will conclude that Yes webelement is not found and your test is pass. If you use implicit wait, it will wait till your specified timeout occurs not before that. Just imagine you have many such scenarios. There are other disadvantages of using implicit waits as well.

  2. Hi Amod,
    I have used the same code but still getting error:
    org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {“method”:”xpath”,”selector”:”//li[@select-id=’results[0]’]”}

    I have checked the Xpath as well it is same for this element .
    Please guide me.

  3. Well Explained Amod. But,in one place you used the term explicit wait.
    1)Behaviour of Explicit wait when time runs out.
    Hope it is a typo.

    Thanks,
    Priya.

Leave a Reply