keepcalculator

06 Numbers and Powers

Implicit vs Explicit Wait in Selenium: Key Differences

Card No. 06·82572 · Updated · Standard formula, shown below

Implicit wait and explicit wait in Selenium both pause test execution until a condition is met, but they differ in scope, flexibility, and how they are applied. Implicit wait applies a single timeout to every element lookup in the WebDriver session, while explicit wait targets a specific element or condition with a custom timeout and polling interval. Understanding these differences helps you write faster, more reliable UI tests.

What Is Implicit Wait?

Implicit wait tells the WebDriver to poll the DOM for a certain amount of time when trying to locate an element if it is not immediately available. It is set once using driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)) and applies to all findElement and findElements calls for the entire lifetime of the WebDriver instance. The default polling interval is 500 ms, and the timeout is 0 by default (meaning no wait).

Key characteristics:

  • Global scope – affects every element lookup in the session.
  • Simple to set – one line of code.
  • Only waits for element presence in the DOM, not visibility, clickability, or other states.
  • If the element appears after 2 seconds, the wait stops; if it never appears, it waits the full timeout then throws NoSuchElementException.

Implicit wait is best for pages where elements load at roughly the same speed and you want a safety net for all locators without writing extra code.

What Is Explicit Wait?

Explicit wait is a dynamic wait that you define for a specific condition on a specific element. It uses the WebDriverWait class combined with an ExpectedCondition. For example:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submit")));

This waits up to 10 seconds for the element with id submit to become visible, checking every 500 ms (default). You can customize the polling interval with pollingEvery(Duration.ofMillis(250)).

Key characteristics:

  • Local scope – only applies to the element and condition you specify.
  • Flexible – you can wait for visibility, clickability, staleness, text presence, element count, and more.
  • Can ignore specific exceptions (e.g., NoSuchElementException) while waiting.
  • Throws TimeoutException if the condition is not met within the timeout.

Explicit wait is ideal for elements that load asynchronously (AJAX), appear after animations, or require a specific state (like being enabled).

Key Differences at a Glance

Aspect Implicit Wait Explicit Wait
Scope Global – all elements Local – specific element/condition
Configuration One-time setting Per wait instance
Conditions Only presence in DOM Visibility, clickable, text, etc.
Default polling 500 ms 500 ms (customizable)
Exception on timeout NoSuchElementException TimeoutException
Performance Can slow down all findElement calls Only affects targeted waits

How to Choose Between Them

Use implicit wait as a baseline for pages with predictable load times, but avoid relying on it for dynamic content. Use explicit wait for critical elements that must be in a certain state before interaction. Best practice is to avoid mixing both because they can conflict: if you set an implicit wait of 10 seconds and an explicit wait of 5 seconds, the explicit wait may return early but the implicit wait still applies to the element lookup inside the explicit wait, potentially causing unexpected delays or timeouts.

Common approach:

  • Set a small implicit wait (e.g., 2–5 seconds) as a fallback for unexpected delays.
  • Use explicit waits for elements that are known to load slowly or require specific conditions.
  • If you must use both, set the implicit wait to 0 and rely solely on explicit waits for full control.

Common Pitfalls and Misconceptions

One frequent mistake is assuming implicit wait waits for visibility. It does not – it only checks if the element is present in the DOM. An element can be present but hidden, and click() will fail with ElementClickInterceptedException or ElementNotInteractableException.

Another misconception is that explicit wait replaces implicit wait entirely. They serve different purposes; explicit wait is more precise but requires more code. Also, note that implicit wait applies to findElements too, which can cause tests to hang for the full timeout when checking for absence of an element. In such cases, use explicit waits with ExpectedConditions.numberOfElementsToBeLessThan or similar.

Performance-wise, a long implicit wait (e.g., 30 seconds) will slow down every element lookup that fails, even if the element appears quickly in most cases. This can make test suites unnecessarily slow.

Alternatives and Advanced Options

FluentWait is a more advanced form of explicit wait that allows you to define polling interval and ignore specific exceptions. It is useful when you need to handle intermittent elements or custom conditions. For example:

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(Duration.ofSeconds(30))
    .pollingEvery(Duration.ofMillis(300))
    .ignoring(NoSuchElementException.class);

Another alternative is using ExpectedConditions with lambda expressions in Java or similar in other languages. Some frameworks like Selenium’s Wait interface allow fully custom conditions.

Frequently Asked Questions

Can I use both implicit and explicit wait together?

Yes, but it’s not recommended. Mixing them can cause unpredictable behavior because the implicit wait applies to the element lookup inside the explicit wait’s polling loop. If the implicit wait is longer than the explicit timeout, the explicit wait may not time out as expected, or vice versa. It’s safer to use one or the other, or set implicit wait to 0 when using explicit waits.

What is the default polling interval for explicit wait?

The default polling interval for WebDriverWait is 500 milliseconds. You can change it using .pollingEvery(Duration.ofMillis(250)) to check more frequently, which can make tests faster but increases CPU usage.

Which wait is faster?

Explicit wait is generally faster because it stops as soon as the condition is met, and you can set a shorter polling interval. Implicit wait also stops when the element appears, but it only checks for presence, not visibility, so it may return earlier than expected if the element is present but not yet interactive.

Does implicit wait work with findElements?

Yes, implicit wait applies to findElements as well. This can cause tests to wait the full timeout when verifying that no elements exist, which is often undesirable. Use explicit waits with conditions like numberOfElementsToBe(0) for such checks.

What happens if the element never appears?

With implicit wait, a NoSuchElementException is thrown after the timeout. With explicit wait, a TimeoutException is thrown. The exception type matters if you have catch blocks that handle them differently.

Can I use explicit wait for non-element conditions?

Yes, explicit waits can be used for conditions like checking page title, URL, alerts, frames, and JavaScript execution results. For example, ExpectedConditions.titleContains("Dashboard") or ExpectedConditions.urlToBe("https://example.com").

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →