If you’ve spent any time automating tests with Selenium WebDriver, you’ve probably run into the dreaded “NoSuchElementException” – usually because Selenium tried to interact with an element before it was ready.
To deal with such timing issues, Selenium provides wait mechanisms to synchronize your test script with the browser. The two most common types of waits are implicit wait and explicit wait.
But here’s a question that often comes up in interviews – and it’s also important to understand when working with selenium scripts:
“Can we use implicit and explicit wait together in Selenium?”
It might sound like a simple yes-or-no question, but the real answer is a little more nuanced. In this article, we’ll break it down in the simplest way possible. We’ll go over what each type of wait does, whether it makes sense to use them together, and how it can impact your test automation.
Let’s dive in.
Differences Between Implicit and Explicit Waits
Implicit Wait
- Sets a global wait time for the entire duration of the WebDriver instance.
- It applies to all elements and instructs the WebDriver to wait for a specific amount of time when trying to find an element, before throwing a NoSuchElementException.
- It is straightforward to use, but not as flexible as explicit waits.
Explicit Wait
- Allows you to set a wait time for specific elements based on certain conditions (e.g., visibility, clickability).
- More flexible and powerful, ideal for waiting until a specific condition is met.
- It is used in conjunction with WebDriverWait and ExpectedConditions.
Now, to the main question –
Can we use Implicit and Explicit Waits together?
Technically, we can use both of them together, but it’s not recommended.
It’s not just our opinion—it’s also clearly mentioned in the official Selenium documentation.
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.
Reference link – https://www.selenium.dev/documentation/webdriver/waits/#:~:text=Do%20not%20mix%20implicit%20and%20explicit%20waits
And now, we’re going to put it to the test and prove it.
Code Example
So, here is how the experiment looks like –
- Go to the url – https://testkru.com/Elements/TextFields
driver.get("https://testkru.com/Elements/TextFields");
- Set the implicit wait at 7 seconds
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(7));
- Start the timer
long startTime = System.currentTimeMillis();
- Explicit wait would be 10 seconds
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
- We’ll use a non-existent element – meaning it won’t be present on the page at all.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("nonExistentElement")));
- End the timer and calculate the difference
long endTime = System.currentTimeMillis();
System.out.println("Total wait time: " + (endTime - startTime) / 1000.0 + " seconds");
Whole code –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; public class CodekruSeleniumTest { public static void main(String[] args) { WebDriver driver = new ChromeDriver(); // Open URL // Set an implicit wait (applies to all elements) driver.manage().timeouts().implicitlyWait(Duration.ofSeconds( 7 )); // start timer long startTime = System.currentTimeMillis(); // Locate the element with an explicit wait try { WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds( 10 )); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id( "nonExistentElement" ))); System.out.println( "Element found." ); } catch (Exception e) { System.out.println( "Element not found." ); } // ending the timer and calculating the difference long endTime = System.currentTimeMillis(); System.out.println( "Total wait time: " + (endTime - startTime) / 1000.0 + " seconds" ); driver.quit(); } } |
Output –
Element not found.
Total wait time: 14.629 seconds
So, why did the total wait time end up being around 14 seconds, even though we set the implicit wait to 7 seconds and the explicit wait to 10 seconds?
Here’s what happened –
The explicit wait checks every 500ms (which is the default polling interval) to see if the element becomes visible. It keeps polling until either:
- The condition is met, or
- The total timeout (10 seconds in this case) is reached.
Internally, ExpectedConditions.visibilityOfElementLocated(By) calls the findElement() method.
Now, if an implicit wait of 7 seconds is set, then every time findElement() is called, it will wait up to 7 seconds to locate the element.
Here’s what happened step-by-step:
ExpectedConditions.visibilityOfElementLocated(By)
calls thefindElement()
method for its first polling- The first findElement() call waits up to 7 seconds.
- If the element isn’t found, it waits an additional 500ms (the polling interval) before retrying.
- The explicit wait then tries again, triggering another findElement() call, which again waits 7 seconds.
- At this point, the explicit wait’s total timeout (10 seconds) is also nearly exhausted, and it stops trying.
This is why the overall wait time ends up being more than expected -roughly around 14.5 seconds ( 7s + 500ms + 7s ) in this case.
So, even though we had setup the implicit wait at 7 seconds and explicit wait at 10 seconds, we end up having to wait around 14 seconds before getting an exception.
So, that’s why we shouldn’t be using implicit and explicit waits together.
This is it. We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.
Related Articles –
Wait for element to disappear in Selenium Java with Examples