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

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.

package MixingWaitsExamples;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ImplicitWaitExample {

	WebDriver driver;
	@Test
	public void mixinfWaitsForLocatingElement()
	{
		WebDriverManager.chromedriver().setup();
		driver = new ChromeDriver();
		// Setting up implicit wait
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		driver.get("http://www.demoqa.com/");
		// Timer starts
		System.out.println("Wait starts at : "+new Date());
		// Locating wrong element
		driver.findElement(By.id("SomeWrongId"));
	}
	
	@AfterMethod
	public void printExitTime()
	{
		System.out.println("Wait ends at : "+new Date());
		driver.quit();
	}
}

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.

package MixingWaitsExamples;

import java.time.Duration;
import java.util.Date;
import java.util.function.Function;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExplicitWaitWithDefaultPollingIntervalExample {

	WebDriver driver;
	@Test
	public void mixinfWaitsForLocatingElement()
	{
		// Initializing browser
		WebDriverManager.chromedriver().setup();
		driver = new ChromeDriver();
		
		// Loading URL
		driver.get("http://www.demoqa.com/");
		
		// Setting up explicit wait with default polling interval
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
		
		// Timer starts
		System.out.println("Wait starts at : "+new Date());
		// Locating wrong element
		wait.until(new Function() {
			@Override
			public WebElement apply(WebDriver driver) {
				System.out.println("Retrying at : "+new Date());
				return driver.findElement(By.id("sdfds"));
			}
		});
	}
	
	@AfterMethod
	public void printExitTime()
	{
		System.out.println("Wait ends at : "+new Date());
		driver.quit();
	}
}

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.

package MixingWaitsExamples;

import java.time.Duration;
import java.util.Date;
import java.util.function.Function;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExplicitWaitWithDivisibleIntervalExample {

	WebDriver driver;
	@Test
	public void mixinfWaitsForLocatingElement()
	{
		// Initializing browser
		WebDriverManager.chromedriver().setup();
		driver = new ChromeDriver();
		
		// Loading URL
		driver.get("http://www.demoqa.com/");
		
		// Setting up explicit wait with custom polling interval
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
		wait.pollingEvery(Duration.ofSeconds(2));
		
		// Timer starts
		System.out.println("Wait starts at : "+new Date());
		// Locating wrong element
		wait.until(new Function() {
			@Override
			public WebElement apply(WebDriver driver) {
				System.out.println("Retrying at : "+new Date());
				return driver.findElement(By.id("sdfds"));
			}
		});
	}
	
	@AfterMethod
	public void printExitTime()
	{
		System.out.println("Wait ends at : "+new Date());
		driver.quit();
	}
}

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

package MixingWaitsExamples;

import java.time.Duration;
import java.util.Date;
import java.util.function.Function;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class ExplicitWaitWithNonDivisibleInterval {

	WebDriver driver;
	@Test
	public void mixinfWaitsForLocatingElement()
	{
		// Initializing browser
		WebDriverManager.chromedriver().setup();
		driver = new ChromeDriver();
		
		// Loading URL
		driver.get("http://www.demoqa.com/");
		
		// Setting up explicit wait with custom polling interval
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
		wait.pollingEvery(Duration.ofSeconds(6));
		
		// Timer starts
		System.out.println("Wait starts at : "+new Date());
		// Locating wrong element
		wait.until(new Function() {
			@Override
			public WebElement apply(WebDriver driver) {
				System.out.println("Retrying at : "+new Date());
				return driver.findElement(By.id("sdfds"));
			}
		});
	}
	
	@AfterMethod
	public void printExitTime()
	{
		System.out.println("Wait ends at : "+new Date());
		driver.quit();
	}
}

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.

package MixingWaitsExamples;

import java.time.Duration;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class MixingOfWaitsForLocatingElement {

	WebDriver driver;
	
	@Test
	public void mixinfWaitsForLocatingElement()
	{
		WebDriverManager.chromedriver().setup();
		driver = new ChromeDriver();
		
		// Setting up implicit wait
		driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
		
		// Load URL
		driver.get("http://www.demoqa.com/");
		
		// Setting up explicit wait with polling interval as 12 seconds
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
		wait.pollingEvery(Duration.ofSeconds(6));
		
		// Timer starts
		System.out.println("Wait starts at : "+new Date());
		
		wait.until(new Function() {
			@Override
			public WebElement apply(WebDriver driver) {
				System.out.println("Retrying at : "+new Date());
				// Implicit wait will be applicable here
				return driver.findElement(By.id("sdfds"));
			}
		});
	}
	
	@AfterMethod
	public void printExitTime()
	{
		System.out.println("Wait ends at : "+new Date());
		driver.quit();
	}
}

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.

package MixingWaitsExamples;

import java.time.Duration;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import io.github.bonigarcia.wdm.WebDriverManager;

public class MixinfWaitsForLocatingElement {

	WebDriver driver;

	@Test
	public void mixinfWaitsForLocatingElement() {
		// Setting up browser
		WebDriverManager.chromedriver().setup();
		driver = new ChromeDriver();

		// Setting up implicit wait greater than explicit wait
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

		// Load URL
		driver.get("http://www.demoqa.com/");

		// Setting up explicit wait with default polling interval
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

		// Timer starts
		System.out.println("Wait starts at : " + new Date());

		wait.until(new Function() {
			@Override
			public WebElement apply(WebDriver driver) {
				System.out.println("Retrying at : " + new Date());
				// Implicit wait will be applicable here
				return driver.findElement(By.id("sdfds"));
			}
		});
	}

	@AfterMethod
	public void printExitTime() {
		System.out.println("Wait ends at : " + new Date());
		driver.quit();
	}
}

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.

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

  1. Hi Amod, in the last case u said that when imp wait is 20 and exp wait is 10, it did not effect the exp wait.But that’s not true isn’t it. because had the imp wait not been declared, then the exp wait would have polled every 500ms. because of imp wait, i directly checked at the end of 10sec. which is not good in a scenario where the element became visible in 2 seconds.

  2. 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.

  3. 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.

  4. 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. Why you are providing wait in test base. Put in utility method and make it customised and override as required

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

  5. 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

  6. 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.

  7. Hi Amod,

    What happend in below scenarios,

    Implicit wait(10) < Explicit Wait(30)

    And element will found in 15 seconds ?

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

  9. Very nice explanation Amod. Your 9 hours of effort is highly appreciated. Keep up the good work..!!

  10. 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 to Neeraj Kumar Cancel reply

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