We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Understand why waits are critical for stable tests and overview of Selenium's wait mechanisms
When testing modern web apps, elements often appear later (after AJAX calls, animations, or SPA updates). If Selenium runs faster than the UI, you get flaky tests and random failures.
Waiting strategies solve this by telling Selenium "don't rush, wait until X is true".
A global timeout applied to all findElement calls.
Set once, applies everywhere
Targeted waits for specific conditions using WebDriverWait.
Wait for specific elements/conditions
Customizable explicit wait with polling intervals and exception handling.
Fine-tuned control over retries
| Feature | Implicit Wait | Explicit Wait | FluentWait |
|---|---|---|---|
| Scope | Global (all findElement) | Specific elements | Specific elements |
| Configuration | One-time setup | Per wait instance | Highly customizable |
| Polling Interval | Fixed (500ms) | Fixed (500ms) | Configurable |
| Exceptions | Can't customize | Can't customize | Can ignore specific ones |
| Use Case | Simple apps | Dynamic apps (recommended) | Complex/flaky scenarios |
| Complexity | ★ Easy | ★★ Moderate | ★★★ Advanced |
driver.manage()
.timeouts()
.implicitlyWait(
Duration.ofSeconds(10)
);
// Now all findElement calls
// will wait up to 10s
driver.findElement(
By.id("btn")
);WebDriverWait wait =
new WebDriverWait(
driver,
Duration.ofSeconds(5)
);
wait.until(
ExpectedConditions
.visibilityOf(
element
)
);Wait wait =
new FluentWait(driver)
.withTimeout(
Duration.ofSeconds(5)
)
.pollingEvery(
Duration.ofMillis(300)
)
.ignoring(
NoSuchElementException
.class
);For modern web apps, use explicit waits with Expected Conditions as your default strategy. They're more precise and readable than implicit waits.
Never use Thread.sleep() for waiting. It makes tests slower and less reliable. Always use proper Selenium waits that retry until conditions are met.
Begin with short timeouts (3-5 seconds) and increase only if necessary. Long timeouts hide performance issues and slow down failing tests.