How to use implicit wait in selenium webdriver and why

There are 2 types of waits available in Webdriver/Selenium 2 software testing tool. One of them is Implicit wait and another one is explicit wait. Both (Implicit wait and explicit wait) are useful for waiting in WebDriver. Using waits, we are telling WebDriver to wait for a certain amount of time before going to next step. We will see about explicit wait in my upcoming posts. In this post let me tell you why and how to use implicit wait in webdriver.
Why Need Implicit Wait In WebDriver
As you knows sometimes, some elements takes some time to appear on software web application page when browser is loading the page. In this case, sometime your webdriver test will fail if you have not applied Implicit wait in your test case. If implicit wait is applied in your test case then webdriver will wait for specified amount of time if targeted element not appears on page. As you know, we can Set default timeout or use “setTimeout” command in selenium IDE which is same as implicit wait in webdriver.
If you write implicit wait statement in you webdriver software testing script then it will be applied automatically to all elements of your test case. I am suggesting you to use Implicit wait in your all test script of software web application with 10 to 15 seconds. In webdriver, Implicit wait statement is as bellow.

How To Write Implicit Wait In WebDriver

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

Above statement will tell webdriver to wait for 15 seconds if targeted element not found/not appears on page. Le we look at simple example to understand implicit wait better.

Copy bellow given @Test method part and replace it with the @Test method part of example given on this page(Note : @Test method is marked with pink color in that example).

@Test
 public void test () 
  {  
   driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
   driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
   driver.findElement(By.xpath("//input[@name='namexyz']"));
  }

In Above webdriver test case with JUnit example,  1st Element ‘xpath(“//input[@name=’fname’]”)’ will be found on page but element xpath(“//input[@name=’namexyz’]”) is not there on page. So in this case webdriver will wait for 15 to locate that element on page because we have written implicit wait statement in our code. At last webdriver test will fail because xpath(“//input[@name=’namexyz’]”) is not on the page.

17 thoughts on “How to use implicit wait in selenium webdriver and why

  1. I have a doubt. If i put implicit wait for 30 seconds and then if webdriver searches at the first time and if element not present then will it wait for total 30 seconds and then search for the second time or it will look for it immediately at 10 or 15 second if the element appears??

    if someone answers this it would be great help. Thanks!

  2. in implicit wait, if webdriver cannot find webelement in starting, it will wait for specified time duration. Webdriver will not search during this wait interval. Once specified time is over, it will try to search again for last time before throwing any exception.
    In Fluent wait and explicit wait, if wait time is 30 seconds, webdriver tries to search for element after some specified time say 500 miliseconds

  3. Awesome…It's works perfectly fine with me. Thread sleep not works for me at all also i tried to driver wait but no luck. I just placed driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS) and magic.

  4. Awesome…It's works perfectly fine with me. Thread sleep not works for me at all also i tried to driver wait but no luck. I just placed driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS) and magic.

  5. Implicit & Explicit Wait time not working. Only thread.sleep(10); works
    It takes to long to put thread.sleep(10); after every line
    Any suggestions.
    driver.manage().timeouts().implicitlyWait(driver.wait(10,TimeUnit.SECONDS));
    WebDriverWait wait = new WebDriverWait(driver, 10);

Leave a Reply

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