Selenium Simplified — Understanding Waits in Selenium (Implicit vs Explicit Wait)
Source: Dev.to
Introduction
In the previous article we learned how Selenium finds elements on a webpage using locators.
Beginners often encounter errors such as NoSuchElementException or ElementNotInteractableException even though the element exists on the page.
The cause is usually timing. Modern web applications load elements dynamically, so Selenium may try to interact with an element before it appears. Selenium executes scripts much faster than a real user:
- Open page
- Find element
- Click button
If the page is still loading content in the background, Selenium can try to locate an element that hasn’t appeared yet, leading to test failures.
Waits allow Selenium to pause until a condition is met (e.g., an element becomes visible, clickable, or the page finishes loading). Once the condition is satisfied, Selenium continues executing the script.
Selenium provides two main types of waits:
- Implicit Wait
- Explicit Wait
Implicit Wait
An implicit wait tells Selenium to wait for a specified amount of time when searching for elements. If the element is not found immediately, Selenium keeps trying until the timeout is reached.
// Set implicit wait to 10 seconds
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
What this means
- Selenium will wait up to 10 seconds when trying to locate an element.
- If the element appears earlier, Selenium proceeds immediately.
driver.findElement(By.id("loginButton")).click(); // waits up to 10 s if needed
If the element takes 3 seconds to load, Selenium waits those 3 seconds and then clicks it.
Explicit Wait
An explicit wait is more flexible. It allows Selenium to wait for specific conditions such as visibility, clickability, or presence of text.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("loginButton"))
);
Here Selenium waits until the element becomes visible on the page.
Common Expected Conditions
visibilityOfElementLocatedelementToBeClickablepresenceOfElementLocatedtitleContains
Example
wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));
This ensures the element is clickable before interacting with it.
Comparison: Implicit vs. Explicit Wait
| Feature | Implicit Wait | Explicit Wait |
|---|---|---|
| Scope | Applies globally | Applies to specific elements |
| Flexibility | Limited | Very flexible |
| Usage | Simple | More control |
| Recommended for complex scenarios | No | Yes |
In most modern automation frameworks, explicit waits are preferred because they provide better control over test behavior.
Why Not Use Thread.sleep()?
Some beginners use Thread.sleep() to pause tests:
Thread.sleep(5000); // pause for exactly 5 seconds
Problems with this approach
- Makes tests slow
- Not reliable (fails if the element loads later)
- Hard to maintain
Using Selenium’s built‑in waits is a much better solution.
Typical Selenium Test Flow with Waits
Open Page
↓
Find Element
↓
Wait Until Element Is Ready
↓
Perform Action
Incorporating waits makes automation scripts more stable and reliable. Timing issues are one of the most common causes of Selenium test failures. Proper use of waits ensures Selenium interacts with elements only when they are ready, resulting in tests that are:
- More reliable
- Less flaky
- Easier to maintain
Series Recap & What’s Next
So far in this series we have covered:
- Selenium architecture
- Locators
- Waits
Next article: Selenium Simplified — Handling Alerts, Frames, and Multiple Windows
These features are common in modern web applications and require Selenium to switch control between different contexts.