Implicit Waits vs Explicit Waits
Why Waits Are Necessary
Modern web pages load content asynchronously, meaning elements may not be immediately available when a script tries to interact with them. Waits tell Selenium to pause intelligently until elements are ready, preventing flaky test failures.
Implicit Waits
An implicit wait applies a single default wait time across the entire script - Selenium will keep polling for an element up to that timeout before throwing an error, whenever any element lookup is performed.
Explicit Waits
An explicit wait targets a specific element and condition, such as waiting until it becomes clickable or visible, rather than applying a blanket timeout everywhere. This makes tests more precise and often faster overall.
Implicit vs Explicit at a Glance
| Aspect | Implicit Wait | Explicit Wait |
|---|---|---|
| Scope | Applies globally to all lookups | Applies to a specific element/condition |
| Flexibility | Low - one fixed timeout | High - custom conditions per element |
| Recommended Use | Simple scripts, quick setup | Real-world, production-grade tests |
Avoiding Mixed Waits
Mixing implicit and explicit waits in the same script can cause unpredictable, inconsistent timeout behavior. Most experienced Selenium users pick one strategy - typically explicit waits - and stick with it consistently.
Writing Both Waits in Selenium 4 (Java)
An implicit wait is set once, usually right after creating the driver:
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.findElement(By.id("username")).sendKeys("demo");
An explicit wait is created where you need it and given a condition:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement submit = wait.until(
ExpectedConditions.elementToBeClickable(By.id("submit")));
submit.click();
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".spinner")));
wait.until(ExpectedConditions.urlContains("/dashboard"));
Selenium 4 uses Duration objects; the older constructors that took a plain number of seconds are deprecated. Python offers the same tools through WebDriverWait and the expected_conditions module.
What Happens While Selenium Waits
- The explicit wait checks the condition immediately.
- If the condition is not met, it sleeps for the polling interval, which is half a second by default, and checks again.
- Exceptions such as
NoSuchElementExceptionduring these checks are ignored, so the wait keeps trying. - As soon as the condition returns a value, the wait stops and returns it, so a fast page is not slowed down.
- If the timeout passes first, a
TimeoutExceptionis thrown with a message describing the condition.
An implicit wait works differently: it only affects findElement and findElements, and only waits for presence in the DOM. It cannot wait for an element to become visible, enabled or clickable.
Why Mixing Them Causes Strange Timeouts
Suppose the implicit wait is 10 seconds and you use an explicit wait of 15 seconds for a spinner to disappear. Each poll of the explicit wait calls findElement, and that call itself may block for up to 10 seconds. The total wait can stretch far beyond what either setting suggests, and the exact time varies by driver. Waiting for something to disappear is the worst case, because every lookup for a missing element waits the full implicit time.
The practical fix is simple: keep the implicit wait at zero and use explicit waits everywhere, ideally through a small helper method so tests stay short.
Choosing a Wait and Mistakes to Avoid
| Situation | Best choice |
|---|---|
| Element appears after an AJAX call | Explicit wait for visibility |
| Button enabled after a form becomes valid | Explicit wait for clickability |
| Custom rule, such as text changing to "Done" | FluentWait or a lambda condition |
| Quick throwaway script | Implicit wait is acceptable |
- Using
Thread.sleep()to fix flakiness; it always waits the full time and still fails on a slow day. - Setting very long timeouts that hide real performance problems.
- Waiting for presence when the test really needs visibility or clickability.
Ready to master Selenium Training Course?
Join Uncodemy's hands-on training and build real automation skills with expert mentors.