Synchronization Commands: Thread.sleep(), Implicit Wait, Explicit Wait
Thread.sleep()
Pauses execution for a fixed duration regardless of whether the page is ready, e.g. Thread.sleep(3000); — simple but wasteful and unreliable, generally discouraged.
Implicit Wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Applies a default wait time across all element lookups for the whole driver session, polling repeatedly until the element appears or the timeout expires.
Explicit Wait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submit")));
Waits for a specific condition on a specific element, giving far more precise control than implicit waits — the generally recommended approach for reliable synchronization.
Why Scripts Fail Without Synchronization
Selenium sends commands far faster than a browser can load pages, run JavaScript or finish AJAX calls. If a script looks for a button before it exists, Selenium throws NoSuchElementException. If the button exists but is hidden or covered by a loader, ElementNotInteractableException or ElementClickInterceptedException appears instead. Waits close the gap between script speed and page readiness.
Each kind of wait handles this differently:
- Thread.sleep always pauses the full time, even if the element was ready after a fraction of a second.
- Implicit wait only affects
findElementandfindElements; it checks for presence in the DOM, not visibility or clickability. - Explicit wait repeatedly evaluates a condition, by default every half second, and returns as soon as the condition is true.
Fluent Wait and Useful Conditions
WebDriverWait is actually a ready-made fluent wait. When you need custom polling or want to ignore specific exceptions, build one directly:
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(20))
.pollingEvery(Duration.ofMillis(500))
.ignoring(NoSuchElementException.class);
WebElement result = wait.until(d -> d.findElement(By.id("result")));
Commonly used conditions from ExpectedConditions include:
| Condition | Typical use |
|---|---|
elementToBeClickable | Buttons and links that are disabled until a form is valid |
visibilityOfElementLocated | Messages and panels that appear after an action |
invisibilityOfElementLocated | Waiting for a spinner or overlay to disappear |
textToBePresentInElementLocated | Status labels that change from "Processing" to "Done" |
urlContains | Confirming navigation after login or submit |
alertIsPresent | JavaScript alerts that open after a delay |
Worked Example: Waiting Out a Loading Spinner
A search page shows an overlay while results load. Clicking the first result too early hits the overlay. A reliable version looks like this:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
driver.findElement(By.id("search")).sendKeys("laptop");
driver.findElement(By.id("go")).click();
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector(".loader")));
WebElement first = wait.until(
ExpectedConditions.elementToBeClickable(By.cssSelector(".result a")));
first.click();
wait.until(ExpectedConditions.urlContains("/product/"));
Every wait states what the script is waiting for, so a timeout message tells you exactly which step was slow.
Mistakes That Make Tests Flaky
- Mixing implicit and explicit waits. The two can combine in unpredictable ways and lengthen timeouts. Pick explicit waits and leave the implicit wait at zero.
- Sleeping "just to be safe". A few three-second sleeps across hundreds of tests add a lot of runtime and still fail on a slow day.
- Waiting for presence when you need visibility. An element can be in the DOM but hidden.
- Holding stale references. If the page re-renders, re-locate the element after the wait instead of reusing an old
WebElement. - Huge timeouts. Very long waits hide real performance problems; use realistic limits.
A practical rule: put waits inside page-object methods, such as clickLogin(), so every test that uses them is synchronized automatically.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.