Make Selenium Easy

Are You Mixing Waits In Selenium WebDriver? – A Bad Practise

Last updated on July 13th, 2020 at 02:21 am

Selenium WebDriver provides two types of waits mechanisms:-

  1. Implicit Wait
  2. Explicit Wait – WebDriverWait and FluentWait

Implicit wait applies for a session of WebDriver and comes in to effect when WebDriver is trying to locate a web element or a list of web elements. Generally, we have a common practice of setting up implicit wait when we initialize a browser and use Explicit Wait wherever needed. But this we are doing wrong. We are mixing up waits which may end up with unexpected wait time.

There is no overriding of waits in Selenium WebDriver i.e. implicit wait will not override explicit wait or vice versa. In fact if you have both waits applied then both waits will be applicable in common scenarios.

Official website of Selenium also says the same:-

Warning: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.

Let’s see this behavior using some examples so that we can understand it better.

Using only implicit wait

In the below example, I will set an implicit wait and will try to locate a web element with wrong ID value.

Output

It should throw NoSuchElementException after 30 seconds i.e. implicit wait timeout.

Using only Explicit Wait

Without polling interval

You will see WebDriver will poll at an interval of 500 MS for 10 seconds and will throw timeout exceptions as there is no such element.

Output

With polling interval

This time I will set a polling interval as well. Explicit timeout as 10 seconds with polling interval of 2 seconds.

Output

Now WebDriver will poll to check condition on an interval of 2 seconds and terminate after 10 seconds.

In the above example, I have set polling interval 2 seconds which divides explicit timeout 10 seconds completely ~ 5. Let’s set a polling interval that is not completely divisible by the explicit wait. For example:- Explicit wait = 20 seconds and polling interval = 6 seconds

Output

Did you observe something strange? Defined Explicit wait timeout was 20 seconds but actually it took 24 seconds. In console log, it appeared as 20 seconds only but you get the actual time taken by taking differentiation of starting and ending time printed above.

Explicit wait with polling interval and time taken to evaluate expected condition may make actual explicit timeout longer. You can go through with some scenarios here:-

Working Mechanism Of Polling Interval In Explicit Wait – Selenium WebDriver

Let’s mix implicit and explicit wait and observe the behavior.

Output:-

Actual explicit timeout is taken:- 26 Seconds (Greater than defined explicit wait 20 seconds). The first time condition was checked at 36th seconds and the second was at 47 seconds. The difference is of 11 seconds which is a sum of 5 seconds (time taken by the findElement method because of an implicit wait) and 6 seconds (defined polling interval). I have explained this logic with examples here.

You can see how a delay in evaluating an expected condition due to implicit wait and polling interval (main reason) increased explicit timeout. This is the reason it is said do not mix implicit wait and explicit wait. It will impact if a scenario where both waits are applicable as discussed above. Custom polling interval restricts WebDriver to check timeout which causes increased explicit timeout.

Output

You can see this time implicit wait did not impact explicit timeout and timed out after 10 seconds with the message “Supplied function might have stalled”.

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

51 thoughts on “Are You Mixing Waits In Selenium WebDriver? – A Bad Practise

  1. Hi Amod,

    In the above codes , we have implicit wait then we have explicit wait. when we debug codes in selenium we see that the code works sequentially. ie it will first execute implicit wait and then explicit wait. so here, how are we mixing implicit andexplicit wait

    1. Hi,
      We set implicit wait to apply max wait till the element is located. We write that line above so that it should be applicable for findElement/s. It doesn’t mean that wait statement executed before will have no impact.

  2. Hi Amod/All,

    In second/third scenario u have given a wrong conclusion

    Considering following case no. 2 :

    If u have Implicit wait 10 sec

    And Explicit wait 5 sec
    And search criteria find element by locator only

    //Don’t use any try catch and see what console says in exception

    console will directly say it has waited 5 seconds only for that Element

    You have given 10 seconds of timeout but it has waited only 5 seconds and rest 5 seconds it has waited because both the waits are triggered Same time but not for that Element.

    In third scenario u have taken search criteria as driver.findelement so in same case by default ur webelemnt follows implicit wait

    Conclusion : in second case it has waited 5 seconds only and not 10 seconds.

    In third case if u will take search criteria as driver.findelement it will by default take implicit wait timing even though explicit wait is greater than implicit.

    This is as per my observations and analysis please correct me if someone has other observations/analysis/results

    Thanks :
    Omkar

    1. Hi Omkar,
      Final conclusion is that it will be messed up when you mix both waits. Your above conclusions may reverse when you run the same tests multiple times. So it is really difficult to say what will be exact timeout.

  3. In my selenium hybrid framework i have a testbaseclass which has an implicit wait defined. now all other classes extends this testbase class. if i need to give an explicit wait for one class say class B and given that mixing waits is not good idea… is there a way to comment implicit wait fr class B?

      1. You should create a method for setting explicit wait in ur framrwork. Call and set wait value as required in ur test scripts.

  4. Hi,

    I tried the same thing for IW > EW and IW< EW , My results are exactly opposite to your results .

    PFB code :

    driver.get("https://www.google.com&quot;);

    WebDriverWait wait = new WebDriverWait(driver, 30);
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    Stopwatch stopwatch = Stopwatch.createStarted();
    try{
    driver.findElement(By.id("lst-ibfgdfg"));
    }
    catch(Exception e)
    {System.out.println("Selenium wait for this run was ########################## : " +stopwatch.elapsed(TimeUnit.SECONDS));
    stopwatch.stop();
    }

    try{
    stopwatch.reset();
    stopwatch.start();
    wait.until(ExpectedConditions.invisibilityOf(driver.findElement(By.id("lst-ib"))));

    }

    catch (Exception e)
    {
    stopwatch.stop();
    System.out.println("Selenium wait for this run was ########################## : " +stopwatch.elapsed(TimeUnit.SECONDS));
    }

    try{
    stopwatch.reset();
    stopwatch.start();
    driver.findElement(By.id("lst-ibfgdfg"));
    }
    catch(Exception e)
    {System.out.println("Selenium wait for this run was ########################## : " +stopwatch.elapsed(TimeUnit.SECONDS));
    stopwatch.stop();
    }
    driver.quit();
    }
    }

    RESULTS are :

    Selenium wait for this run was ########################## : 15
    Selenium wait for this run was ########################## : 30
    Selenium wait for this run was ########################## : 15

  5. Hi Amod,

    Thanks for the explanation. I had faced the same question in interview, fortunately I was correct.

    But, I have a doubt, if it is not a good idea to use Implicit wait then everyone should have used explicit wait only, when they to go for Implicit wait? In what case we need to use it?

    Thanks in advance!

    Thank you,
    Shriniwas

    1. Hello,
      It comes with experience. Every wait has its advantages. Explicit and fluent waits are more efficient. People use as per their convenient. Implicit you need to define once but explicit wait is exclusive for an action. You need to always extra codes for explicit wait.

  6. Hello Amod,

    I have also dove same thing and findings are same .in every case implicit wait will ruin the explicit wait . Then what is the solution or workaround.how can we will use both in single code?

    Thanks In Advance

      1. Hello Amod,

        I found 1 more thing there are 2 ways to set condition in explicit wait

        1.wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector(“input.newsrchbtn11”)));

        2.wait.until(ExpectedConditions.presenceOfElementLocated((By)driver.findElement(By.cssSelector(“input.newsrchbtn11”))));

        if i use 1st line code to set condition then Explicit wait will work correctly bcs in 1st wait m not using driver.findelement , So implicit wait will not work .

        But if i will go with second line of code then only implicit wait will ruin explicit wait.

  7. It would be better if you elaborate the conclusion. Specially 2nd point where implicit wait greater than explicit wait, how it results into exception..?

    1. Because I amhave given wrong loctor so that it should throw an exception. And conclusion I have given clearly. Where you have doubt??

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Are You Mixing Waits In Selenium WebDriver? – A Bad Practise

by Amod Mahajan time to read: 6 min
51
%d bloggers like this: