Introduction
In the previous article, we learned how Selenium finds elements on a webpage using locators.
But beginners often run into a frustrating error when running their tests:
NoSuchElementException
or
ElementNotInteractableException
Even though the element exists on the page.
So what’s happening?
The problem is usually timing.
Modern web applications load elements dynamically, which means Selenium might try to interact with an element before it appears on the page.
This is where waits become important.
Why Waits Are Necessary
When Selenium executes a script, it runs very fast.
Much faster than a real user.
For example:
Open page
Find element
Click button
But the webpage may still be loading content in the background.
So Selenium may try to locate an element that has not appeared yet.
This causes test failures.
Waits allow Selenium to pause until the element is ready.
How Waits Work in Selenium
Instead of immediately failing, Selenium can wait for a condition to be met, such as:
- An element becoming visible
- An element becoming clickable
- A page finishing loading
Once the condition is satisfied, Selenium continues executing the script.
Types of Waits in Selenium
Selenium provides two main types of waits:
- Implicit Wait
- Explicit Wait
Let’s understand both.
Implicit Wait
An implicit wait tells Selenium to wait for a certain amount of time when searching for elements.
If the element is not found immediately, Selenium will keep trying until the timeout is reached.
Example
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 continues immediately.
Example usage
driver.findElement(By.id("loginButton")).click();
If the element takes 3 seconds to load, Selenium waits and then clicks it.
Explicit Wait
An explicit wait is more flexible.
It allows Selenium to wait for specific conditions.
Examples:
- Wait until an element is visible
- Wait until an element is clickable
- Wait until text appears
Example
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 Explicit Wait Conditions
Some frequently used conditions include:
visibilityOfElementLocated
elementToBeClickable
presenceOfElementLocated
titleContains
Example:
wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));
This ensures the element is clickable before interacting with it.
Implicit Wait vs Explicit Wait
Here is a simple comparison:
| 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 allow better control over test behavior.
Common Beginner Mistake
Many beginners use Thread.sleep() to pause tests.
Example:
Thread.sleep(5000);
This forces the script to stop for exactly 5 seconds, even if the element loads earlier.
Problems with this approach:
- Makes tests slow
- Not reliable
- Hard to maintain
Using Selenium waits is a much better solution.
A Simple Wait Flow
This is how waits fit into a Selenium test flow:
Open Page
↓
Find Element
↓
Wait Until Element Is Ready
↓
Perform Action
This makes automation scripts more stable and reliable.
Final Thoughts
Timing issues are one of the most common causes of Selenium test failures.
Using waits correctly helps ensure that Selenium interacts with elements only when they are ready.
This makes tests:
- More reliable
- Less flaky
- Easier to maintain
Up Next in This Series
So far in this series we have covered:
- Selenium architecture
- Locators
- Waits
Next, we will learn how Selenium handles special browser contexts.
In the 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.
Top comments (0)