Advance Selenium, Wait in Selenium

How to use Implicit wait in Selenium Webdriver

Selenium

This guide will help you to understand implicit wait in Selenium Webdriver and implementation as well. If you have ever worked on any automation tool then you must be aware of Sync issues in the script.

It is one of the biggest pain for an automation engineer to handle sync issues between elements.

Sometimes an application will not able to load elements due to the below issues.

  • Network issue
  • Application issues
  • Browser stopping JavaScript call.

And so on.

By default, Selenium will not wait for an element once the page load completes. It checks for an element on the page then it performs some operation based on your script but if the element is not present then it throws NoSuchElement Exception.

 

 

This is one of the interview questions that what is the default timeout in Selenium is.

Answer- The default timeout is ZERO.

 

I have listed down some most frequent exception of Selenium. I hope it will help you.

Frequently faced exception is Selenium Webdriver

 

If you are not sure how to handle exception then the below post will help you.

How to handle exception in Selenium Webdriver

 

Our main intention to use Implicit wait in selenium is to instruct Webdriver that waits for a specific amount before throwing an exception.

I will highly recommend using implicit wait in your every selenium script or if u have created framework then add the same in BaseClass or Utility

Note- Implicit wait in selenium webdriver will be applicable throughout your script and will works on all elements in the script once your specified implicit wait. It is also known as Global wait

 

Syntax of Implicit wait in selenium webdriver

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


 

Here in above example, I have used TimeUnit as seconds but you have so many options to use

Seconds, Minutes, Days, Hours, Microsecond, Milliseconds, and so on check the below screenshot for more information.

 

Implicit wait in Selenium

 

 

Once you move forward, you will get to know some more Timeout in Selenium like

Page load timeout in Selenium

Script load timeout in Selenium

Implicit wait in selenium Webdriver (Current reading)

Explicit wait in Selenium

Fluent wait in Selenium

Do not confuse with these exceptions because each of this having different usage.

 

Complete program with usage of implicit wait in selenium webdriver



import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;


public class VerifyTitle

{

                @Test

                public void verifySeleniumTitle()

                {

                                WebDriver driver=new FirefoxDriver();

                                driver.manage().window().maximize();

                                driver.get("https://vistasadprojects.com/mukeshotwani-blogs-v2");

                                // Specify implicit wait of 30 seconds                                   
                                 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                              // No login id is present on Webpage so this will fail our script.
                                driver.findElement(By.id("login")).sendKeys(" Selenium Webdriver");

                }

}


 

Console- The above script will fail because no login id is present on the page but it will wait for max 30 seconds before throwing an exception.

 

implicit wait in Selenium webdriver

Note- This is the max time so if any element is coming before 30 seconds it will move to next element.

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

26 thoughts on “How to use Implicit wait in Selenium Webdriver

  1. Abhinav says:

    while iam trying to run my java application in eclipse iam getting an error saying that “could not load main class” . can you please help me out with this.

    1. Hi Abhinav,

      Are you using @Test annotation from TestNG/JUnint? If not, then you need to use main method to execute your code

  2. Siddharth Barthwal says:

    what is the polling time for implicit wait. Does it recheck after 10 seconds if 10 seconds is the max time mentioned.

    1. Hi Siddharth,

      It is 0.5 second

  3. venkatesh says:

    which one will be considered first, when both Implicit and Explicit waits are in picture.
    Some scenarios –
    when Implicit time out is 20 sec and explicit Time out is 30 sec
    What will happen if the element is found out
    1. at 10th sec
    2. at 25th sec
    3. at 31st sec
    4. at 50th sec

    1. Hi Venkatesh,

      First of all Implicit wait will be applied for all elements and Explicit wait works for specific conditions.
      Now let’s talk about condition if element found within 20 sec then it will continue with execution else it will throw noSuchElementException.

      Thanks
      Mukesh Otwani

  4. pooja says:

    Hi,

    is this wait time applicable for the entire script once it is mentioned.
    Like will it wait for the element visibility for each and every element in the script?

    1. Hi Pooja,

      Implicit wait works throughout the script once. But for element visibility, you have to use Explicit wait.

  5. dineshkumar says:

    what happens if element came after 30 seconds it will throw exceptions

    1. Hi Dinesh,

      It will throw an exception

  6. Ekta says:

    Is Implicit wait applicable only for elements which are identified using findElement() method of webdriver instance?

    1. Hi Ekta,

      Yes, Implicit Wait tell WebDriver to poll DOM while finding an element for given time.

  7. Debanjan Bhattacharjee says:

    Brilliant explanation on Implicit Wait. Thanks Mukesh !!!

    1. Thanks Debanjan 🙂 Keep visiting.

  8. Raja says:

    I want to execute the code slowly to observe each step, how can i use wait for it

    1. Hi Raja,

      Set toggle breakpoints inside your script and debug it. This way, you can execute step by step.

  9. pooja says:

    Hi Mukesh,
    Very nice blog i ove it. i have one question.
    Explicity wait will use for all the times like thread.sleep();?

    1. Hi Pooja,

      As the name of wait i.e. Explicit itself explaining its exclusive and specific usage. And no doubt Explicit waits are better than Thread.sleep().

  10. Thanks. It’s helpful and easy to implement and best part was the timeout screenshot that you shared which highlight it weighted for 30 seconds. NewBies generally miss such details. Keep up the good work

  11. sudhr says:

    in tutorial u said that default time is 250 ms.if i mention 30 s. driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); how can this work.what is the total wait…plz explain?

    1. Hi,

      In above scenario if element not found then max time wait will be 30 seconds and if element comes before 30 seconds it will continue to next steps.

  12. sudhr says:

    what default timeout for implicit wait..

    1. Default timeout is ZERO. Default polling time is 250 millisecond if implicit wait given.

      1. venkatesh says:

        what is the polling time for explicit wait?

        1. Hi Venkatesh,

          Default polling for Explicit Wait in 500 milliseconds…

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.