Selenium Explicit Wait in Java: WebDriverWait vs Implicit Wait Guide
Learn why mixing implicit and explicit waits causes unpredictable timeouts, how to use Selenium 4's `Duration.ofSeconds()` WebDriverWait syntax, and how to write custom lambda waits.
Test this solution live in real cloud browsers
Preload the working test script directly into TargetQA's Selenium WebDriver 4 compiler. Zero local dependencies or browser drivers needed.
Understanding the Breakdown
Modern SPAs fetch data asynchronously after clicks and navigation. Calling `driver.findElement()` immediately raises `NoSuchElementException` or `ElementNotInteractableException`, while hardcoding `Thread.sleep(5000)` makes CI pipelines painfully slow and still flaky.
Why Traditional Selectors Fail
Implicit waits only check DOM presence, not visibility or clickability, and mixing implicit + explicit waits compounds timeouts unpredictably across remote WebDriver calls.
Production-Grade Solutions
Choose the strategy that matches your test architecture. Pattern #1 is recommended for most production test suites.
1. WebDriverWait + ExpectedConditions (Selenium 4 Syntax)
RecommendedUse `Duration.ofSeconds()` to wait for visibility, clickability, or text updates on a specific locator.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement btn = wait.until(
ExpectedConditions.elementToBeClickable(By.cssSelector("button[data-testid='checkout']"))
);
btn.click();2. Custom Lambda Wait Condition
Wait on dynamic application state such as a table finishing filtration or a counter reaching a target value.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(d -> d.findElements(By.cssSelector("tbody tr")).size() >= 3);Complete Working Test Script
You can copy this exact test into your repository or run it immediately in our cloud sandbox against live demo targets.
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ExplicitWaitDemoTest {
@Test
void waitForHeadingInCloudChrome(WebDriver driver) {
driver.get("https://demo.targetqa.com");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
var heading = wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("h1")));
assertTrue(heading.isDisplayed());
}
}Best Practices & Golden Rules
- Keep implicit wait at 0 seconds and rely on targeted `WebDriverWait` conditions.
- Always pass `By` locators (not pre-fetched `WebElement` references) into `ExpectedConditions` to avoid stale references.
- Wait for `invisibilityOfElementLocated` on loading spinners before asserting updated table rows.
Common Pitfalls to Avoid
- Using deprecated `new WebDriverWait(driver, 10)` integer constructors removed in Selenium 4.
- Combining global `implicitlyWait(Duration.ofSeconds(10))` with `WebDriverWait`, causing 20+ second hangs on negative assertions.
Frequently Asked Questions
What is the difference between Implicit Wait and Explicit Wait in Selenium?
Implicit wait applies globally to every `findElement` call and only checks DOM presence. Explicit wait (`WebDriverWait`) targets a single step and waits for rich UI states like visibility, clickability, text changes, or URL transitions.
Why did Selenium 4 replace `new WebDriverWait(driver, 10)`?
Selenium 4 standardized all timeouts on `java.time.Duration` (e.g. `Duration.ofSeconds(10)`) for type safety and readability.