Stop Guessing When Elements Appear: Explicit Waits in Selenium 4
Replace sleep statements and implicit waits with WebDriverWait and ExpectedConditions. A concrete example shows how to wait for a clickable login button, choose the right condition, and tune timeouts without slowing the suite.
14 Oct 2025, 11:53 UTC

The Flaky Test Problem
You push a test suite that passes locally. CI runs it and three tests fail—different ones each run. The logs show NoSuchElementException on elements that definitely exist. The culprit: the page hasn't finished rendering when your test tries to interact. Teams often reach for Thread.sleep(5000) or a global implicit wait, both of which paper over the real issue and slow every test down.
Why Explicit Waits Are the Right Abstraction
An explicit wait tells WebDriver: "Poll the DOM until this specific condition is true, but give up after N seconds." It's scoped to one interaction, readable, and fails fast when the condition genuinely isn't met. WebDriverWait combined with ExpectedConditions gives you a vocabulary like presenceOfElementLocated, visibilityOfElementLocated, and elementToBeClickable that maps directly to what your test actually needs.
Choosing the Right Condition
Not all conditions are interchangeable. presenceOfElementLocated only confirms the element exists in the DOM—it may be hidden, zero-size, or covered by an overlay. visibilityOfElementLocated adds display and dimension checks. elementToBeClickable goes further: visible, enabled, and not obscured. Pick the weakest condition that still guarantees your next action succeeds. If you only need to read text, visibility is enough. If you're clicking, wait for clickable.
Worked Example: Login Button That Loads Late
Consider a React app where the login button mounts after an auth check completes. A naive test fails intermittently:
// Flaky: assumes button is immediately present
driver.findElement(By.id("login-btn")).click();
Replace with an explicit wait scoped to this interaction:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement loginBtn = wait.until(ExpectedConditions.elementToBeClickable(By.id("login-btn")));
loginBtn.click();
Run this against a page where the button appears at 3.2 seconds. The test passes consistently. Reduce the timeout to 2 seconds and it fails predictably with TimeoutException, exposing the performance regression immediately.
Timeouts and Polling: Practical Tuning
Default polling is 500 ms. For most web apps that's fine. If you're testing a slow legacy app, bump the timeout to 15–20 seconds but keep polling at 500 ms—don't drop to 50 ms; it spikes CPU on parallel runs. A good starting point: 10 seconds timeout, default polling. Measure your 95th-percentile element load time in staging and set timeout to 2× that value.
Common Pitfalls
- Mixing implicit and explicit waits: Implicit wait is global and adds to explicit wait time unpredictably. Disable it:
driver.manage().timeouts().implicitlyWait(Duration.ZERO); - Waiting for the wrong condition: Waiting for
presenceon a button that's disabled by a loading spinner leads toElementNotInteractableExceptionafter the wait succeeds. - Headless quirks: In headless Chrome,
numberOfWindowsToBe(2)sometimes resolves before the new window's document is ready. Follow with a wait on a known element inside the new window.
Verification Checklist
- Lock Selenium version in your build (e.g.,
io.selenium:selenium-java:4.15.0). - Add a minimal integration test that loads a page with a delayed element and asserts
elementToBeClickablewithin 5 seconds. - Run the suite in parallel locally; watch for CPU spikes from aggressive polling.
- Confirm
TimeoutExceptionmessages include the locator and condition for faster debugging.
Closing Action
Replace every Thread.sleep and global implicit wait in your codebase this sprint. Start with the flakiest test file. You'll cut runtime, eliminate intermittent failures, and gain a shared vocabulary for "wait until ready" that survives refactors.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.