Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/19/2025 By admin

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.

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();
        }
}

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

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(); }
}

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(); }
}

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(); }
} 

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

Uncategorized

Post navigation

Previous Post: TestNG Tutorials 56: DataProvider in TestNG – Parameterizing DataProvider Method to Provide Data to Multiple Test Methods
Next Post: JavaScript Way To Handle Calendar In Selenium

Related Posts

image – Make Selenium Easy Uncategorized
#6. OAuth 2.0 Flow – Role Of Refresh Token in OAuth 2.0 Flow Uncategorized
JSON with JSONPath Uncategorized
PROTRACTOR TUTORIAL 2– Identify Angular and Non-angular Application Uncategorized
Untitled Diagram (7) – Make Selenium Easy Uncategorized
November 2017 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark