Selenium · Browser automation foundations

How do waits work in Selenium and why are sleeps wrong?

MediumwaitswebdriverwaitsynchronizationUpdated Oct 2026
Quiz this track

Short answer

WebDriverWait polls an expected condition until it is true or the timeout expires. It synchronises on application state, while Thread.sleep always waits the full duration and makes tests slow and still flaky.

Modern pages update asynchronously. A click may trigger a request, a re-render and an animation. The test needs to wait for a specific observable state, not for a guessed amount of time. Explicit waits make that state and timeout visible in the test.

java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

WebElement save = wait.until(
    ExpectedConditions.elementToBeClickable(By.cssSelector("[data-testid='save']")));
save.click();

wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("spinner")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[role='status']")));
wait.until(ExpectedConditions.textToBePresentInElementLocated(
    By.cssSelector("[role='status']"), "Saved"));
Each wait describes a concrete application state with an explicit timeout.
  • elementToBeClickable covers presence, visibility and enabled state
  • visibilityOfElementLocated waits for a non-hidden element
  • invisibilityOfElementLocated is useful for spinners and overlays
  • textToBePresentInElementLocated waits for content rather than presence
  • urlContains and titleContains synchronise on navigation
  • stalenessOf helps when a re-render replaces the node you held

Set timeouts from the slowest realistic response in the environment, not from the fastest. A ten second timeout does not make a passing test slower; it only caps how long a failure can hang. When a wait times out, the message should identify the locator and expected state.

Thread.sleep does not synchronise anything. It adds the same delay on every run and still fails when the application is slower than the guess.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Which ExpectedConditions do you use most often?
  2. 2How do you choose a timeout value?
  3. 3Why is waiting for a spinner better than waiting for a fixed delay?
  4. 4What is stalenessOf for?

Say it in the interview

WebDriverWait polls a specific condition until it succeeds or the timeout expires. I wait for the state the test actually depends on, such as a button becoming clickable or a status message appearing. That is both faster and more reliable than a fixed sleep, which adds the same delay every time and still fails when the app is slow. I set the timeout from realistic worst-case latency and make the failure message name the locator.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Selenium mock interview and get scored feedback on depth, clarity and confidence.