[フレーム]

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / How to use Implicit wait in Selenium Webdriver

How to use Implicit wait in Selenium Webdriver

by 26 Comments

[画像: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("http://www.learn-automation.com");
  // 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.

Reader Interactions

Comments

  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.

  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.

  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

    • Mukesh Otwani says

      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?

  5. Ekta says

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

  6. 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();?

  7. Gaurav Khurana says

    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

  8. 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?

Leave a Reply Cancel 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.

AltStyle によって変換されたページ (->オリジナル) /