Different Types Of Wait in Selenium

In the previous post, we have seen the difference between close() and Quit() method of WebDriver. In this post , we will see different types of wait in Selenium.

During Script execution, if elements in the webpage or webpage itself are not loaded or visible completely but Selenium tries to perform some actions upon them, then it will throw “NoSuchElementException because Selenium works on the element which are successfully loaded and visible.This is called Sync issue in the code.

So, we need to wait for desired time during execution until those elements are properly loaded or visible in the page. This sync issue can be resolved by using different wait commands in selenium.

There are 3 types of wait commands that can be used in Selenium.

  1. Implicit Wait
  2. Explicit Wait
  3. Fluent Wait

1. Implicit Wait:

In Selenium WebDriver, the idea of Implicit wait has been influenced from Watir. Implicit wait can accept the time amount in seconds,milliseconds, nanoseconds,minute etc. It depends on your requirement.

Implicit wait sets a default time for selenium WebDriver. An exception will be thrown if it is not able to find out any element within the specified amount of time. This is applicable for each element in the application.

Don't Forget Note Means Important Remember Forgetting

Once implicit wait is set , it will be applicable until the WebDriver instance is destroyed.
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.gmail.com");

 


2. Explicit Wait:

This wait gives you the options to wait until a desired condition is satisfied. You can use some of prebuilt ExpectedConditions for the elements to be visible, invisible,clickable etc.

		
driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10); //Explicit wait for 10 seconds
driver.get("http://www.gmail.com");
		
WebElement username = driver.findElement(By.id("Email"));
wait.until(ExpectedConditions.visibilityOf(username));  //Wait until username is visible

Don't Forget Note Means Important Remember Forgetting 

Once implicit wait is set , it will be applicable until the WebDriver instance is destroyed.

 


3. Fluent Wait:

FluentWait contains 3 parameters.

  • WithTimeOut: defines the maximum amount of time it will wait for any element to be visible.
  • pollingEvery’ : takes time as an argument e.g. 5 seconds. It will check the status of element after every 5 seconds.
  • ignoring: we can define the type of exceptions need to be ignored during element search.
       Wait<WebDriver> wait =  new FluentWait<WebDriver>(driver)
			.withTimeout(30, TimeUnit.SECONDS)  // wait maximum 30 seconds
			.pollingEvery(5, TimeUnit.SECONDS)  // check after every 5 seconds
			.ignoring(NoSuchElementException.class);  // ignore NoSuchElementException

		WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
			public WebElement apply(WebDriver  driver) {
				return driver.findElement(By.id("Email"));
			}
		});

When to use implicit wait, explicit wait and Fluent wait?

Implicit wait: During implicit wait time, it tries to locate the element in the WebPage. If webDriver does not find the element initially. Then it doesn’t try to identify the element again, instead it waits till the specified time is over. In the end moment when time is over, it checks once again if element is present before throwing the exception.The default setting is zero. It is default time which is applied throughout the execution session and only gets destroyed when the WebDriver instance is destroyed.

Explicit Wait: It should be applied for those specific elements which takes more time to be displayed comparing to other elements in the application. Suppose, we have observed that one element is taking around 1 minute time to be displayed. Then we can not apply implicit wait for 1 minute because then it will wait for same amount of time for every element.

Instead, we will use explicit wait with the expected condition.

Fluent Wait: Suppose, there are few elements which sometimes takes more than 1 minute to display or sometimes gets displayed within 2 seconds. For such elements, it would be better to use fluent wait. As it will keep checking the presence of those elements  after a specified amount of time until it finds it or time out.

FluentWait can prove pretty handy for Ajax because of the flexibility you have for ignoring exceptions and easily tweaking the wait and polling times.


 

Ask Question
Have any question or suggestion for us?Please feel free to post in  Q&A Forum
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

3 Responses

  1. Megha says:

    I really appreciate for this valuable post.

  1. August 17, 2015

    […] the previous post, we have seen different types of wait commands in Selenium. In this post, we will see how to work with WebList in […]