skip to content

Explicit Wait Polling

A wait object calls a condition over and over from the client until it returns something usable or the clock runs out. Interviewers probe the interval, the return value and the exceptions.

on this pageshow

explore

questions

4

In Selenium, what happens when a WebDriverWait's condition never returns a usable value before the timeout?

level: juniorimportance: should knowfreq 66%

answer

  1. Unchecked, and thrown from inside until()
  2. Not the java.util.concurrent one
  3. The message names the condition and interval
  4. Look at what is attached underneath
  5. A null cause means something different

basics

~10 s

The wait throws org.openqa.selenium.TimeoutException. Its message names the condition it was waiting on plus the timeout and interval in force, and any exception the last attempt threw is attached as the cause.

solid answer

~40 s

`until()` throws `org.openqa.selenium.TimeoutException`, an unchecked exception extending `WebDriverException` — not `java.util.concurrent.TimeoutException`, and not the `NoSuchElementException` a failed lookup inside the condition may have raised. The message reads `Expected condition failed: <the condition's toString()> (tried for 10 second(s) with 500 milliseconds interval)`, so it names both what was awaited and the timing actually in force. If the last attempt threw an exception the wait was tolerating, that exception becomes the `cause`; if attempts ran cleanly and merely returned `null` or `false`, the cause is `null`. The clock is only checked after an attempt, so the condition is always evaluated at least once and gets a final chance right on the deadline.

go deeper

for a junior

Be ready to name TimeoutException as the failure a wait raises, say that it is unchecked so the test simply fails at that line, and recognise the Expected condition failed prefix in a log.

for a middle

Explain when the throw happens relative to the last attempt, and what the message's trailing timeout and interval figures tell you about how the wait was really configured.

for a senior

Demonstrate that you triage from the exception itself: read the condition text, the configured timing, and above all the cause, using its presence or absence to separate a wrong locator from a slow application.

for a principal

Own the argument that a wait failure should arrive self-describing, and that helpers which swallow or flatten these exceptions cost an organisation far more in reproduction time than they save in tidy code.

## The exception, and which one it is When the deadline passes with no usable result, `until()` throws **`org.openqa.selenium.TimeoutException`**. It is unchecked and extends `WebDriverException`, so nothing forces you to catch it and a test method that calls a wait fails at that line with no extra plumbing. - It is **not** `java.util.concurrent.TimeoutException`. The two share a simple name, and an editor that auto-imports the wrong one produces a `catch` block that never fires. - It is not `NoSuchElementException`. A failed lookup *inside* the condition is a different event from the wait giving up, even when the first caused the second. - It is thrown from inside `until()`, so it propagates out of whatever helper wrapped the wait unless that helper catches it deliberately. ## Exactly when the throw happens The loop only ever checks the clock **after** it has evaluated the condition: 1. Apply the condition and inspect the result. 2. If the result is neither `null` nor `false`, return it — no timeout, whatever the clock says. 3. Only now compare the clock against the deadline. 4. If the deadline has passed, throw `TimeoutException`. 5. Otherwise sleep the polling interval and go round again. Two consequences follow, and interviewers like both. First, the condition is **always evaluated at least once**, even with a zero timeout — a wait can succeed on a page that is already in the right state without any clock arithmetic at all. Second, the condition gets one final evaluation after the deadline has technically expired, so a bakery pre-order confirmation that appears during the last sleep is still picked up rather than being lost to a race with the clock. ## What the message carries The message is assembled by the wait itself and is unusually informative for a Selenium failure: ```text org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.id: preorder-confirmation (tried for 10 second(s) with 500 milliseconds interval) ``` - **`Expected condition failed:`** is the fixed prefix, so the string is easy to grep across a run's output. - The middle is the condition's own `toString()`, which is why a well-named condition tells you *what* was being waited on and against which locator. - The trailing parenthetical reports the **timeout and the polling interval that were actually in force** — the quickest way to discover that a wait is not configured the way the code appears to say. - `WebDriverWait` additionally attaches driver class, session id and capabilities to the exception, so the report identifies which browser session gave up. ## The cause is a diagnostic, not noise The wait records the last exception an attempt threw, and if it eventually times out, that exception becomes the **cause** of the `TimeoutException`. Crucially, an attempt that completes cleanly but returns `null` or `false` **clears** the recorded exception. That gives you a two-valued signal for free: | `getCause()` on the `TimeoutException` | What actually happened | |---|---| | a `NoSuchElementException` | every attempt threw not-found — the locator matched nothing, so suspect the selector | | `null` | attempts ran cleanly and simply never returned a usable value — the element exists but never reached the state asked for | That distinction separates "my locator is wrong" from "the application really was too slow", and it is available on the failure you already have, without reproducing anything. ## Reading one in a failure report - Start with the parenthetical: confirm the timeout and interval are what you intended. - Read the condition text next: it names the locator and the state, which is usually enough to know whether the wait was asking the right question about the bakery form. - Then look at the cause. A `null` cause and a wrong-locator hypothesis are inconsistent; a `NoSuchElementException` cause and a "the server was slow" hypothesis are inconsistent too. - Only after those three does a screenshot or page source add anything. ## Catching it deliberately Catching `TimeoutException` is legitimate when the timeout itself is information — for example when a helper wants to add context about which step of the bakery pre-order flow it was driving before rethrowing. Two rules keep that useful: rethrow or chain the original so the message and cause survive, and never swallow it into a bare `return false`, which converts a precise, self-describing failure into a silent one that the next reader has to reconstruct from scratch.

  • What does it mean when the TimeoutException has no cause at all?
    That every attempt ran cleanly and simply returned `null` or `false`. The wait clears its recorded exception whenever an attempt completes without throwing, so a null cause rules out a not-found locator and points at the application never reaching the state the condition asked about.
  • Does a WebDriverWait built with a zero timeout ever evaluate its condition?
    Yes, exactly once. The loop applies the condition first and only then compares the clock to the deadline, so a zero-timeout wait still gets one attempt and can succeed on a page already in the required state. It throws `TimeoutException` only after that single attempt fails to produce a usable value.
  • Why is catching TimeoutException and returning false a poor habit?
    It destroys the most useful failure message Selenium produces. The exception carries the condition text, the timeout and interval in force, the session id and the cause chain; a bare `false` reduces all of that to one bit, and the next reader has to reproduce the failure to learn what the wait already knew.

saying these in an interview costs you the question

  • Names NoSuchElementException as what a wait throws when it times out
  • Thinks the message never says which condition was being awaited
  • Ignores the cause chain when diagnosing a wait failure
  • Believes a zero timeout skips evaluating the condition entirely
  • Confuses it with java.util.concurrent.TimeoutException
open as a page

In Selenium, what does a WebDriverWait do when you call until() with a condition?

level: juniorimportance: should knowfreq 78%

basics

~10 s

WebDriverWait repeatedly applies the condition from the test process until it produces a usable value, which until() then returns. If the deadline passes first, the wait throws TimeoutException instead of returning.

open as a page

Why can a Selenium WebDriverWait with a 10-second timeout take noticeably longer than 10 seconds to fail?

level: middleimportance: should knowfreq 45%

basics

~20 s

The wait checks the clock only between attempts, never during one. It evaluates the condition first, so an attempt that starts just before the deadline runs to completion, and total elapsed time is the timeout plus that final attempt.

open as a page

In Selenium, why does a WebDriverWait on a wrong locator fail with TimeoutException rather than NoSuchElementException?

level: seniorimportance: should knowfreq 52%

basics

~10 s

WebDriverWait registers NotFoundException as ignored, so every not-found thrown inside the condition is caught, recorded and retried until the deadline. The recorded exception then becomes the cause of the TimeoutException the wait finally throws.

open as a page