An element must be both present in the DOM and visible on the page for Selenium WebDriver to interact with it.
When a website loads, it often takes a few seconds for all elements, such as buttons, text fields, or headers, to appear. Similarly, after clicking a tab or a button, the next section of the page might take time to become visible
For example, consider the following steps in a test script:
1. Enter a username
2. Enter a password
3. Click the login button
4. Click a header on the dashboard
If the dashboard takes 2–3 seconds to load after login, Selenium will not wait by default. It will immediately try to locate the dashboard header (step 4) and throw an error saying the element does not exist. Because the element hasn’t loaded yet.
To handle such cases, Selenium WebDriver provides waits and timeouts. These tell WebDriver to "wait until the element becomes available" before performing any action on it.
There are different types of waits in Selenium WebDriver to manage delays caused by dynamic content or page load behavior.
Types of Waits in Selenium WebDriver
Selenium WebDriver provides different types of waits to handle synchronization issues that arise when elements take time to appear or become interactive during test execution
Implicit Wait
An implicit wait tells WebDriver to wait for a certain amount of time when trying to find an element, before throwing a NoSuchElementException. It applies globally and affects all findElement() and findElements() calls.
Syntax for Implicit Wait:
This code tells Selenium WebDriver to wait up to 10 seconds for each element to appear. If it appears earlier, the script proceeds immediately.
Explicit Wait
An explicit wait allows you to wait for a specific condition to occur before proceeding to the next line of code.
Example for Explicit Wait:
This code tells Selenium to wait up to 10 seconds until the element with id “submit” becomes visible on the page, then it performs the click action. Unlike the implicit wait, this code is only applicable to the submit button.
Here are some of the common conditions used with Explicit Wait.
Conditions |
Description |
titleIs(String title) |
Waits until the page title matches exactly the specified string. |
titleContains(String title) |
Waits until the page title contains the specified substring. |
presenceOfElementLocated(By locator) |
Waits until the element is present in the DOM, regardless of its visibility. |
visibilityOfElementLocated(By locator) |
Waits until the element is present in the DOM and visible on the page. |
visibilityOf(WebElement element) |
Waits until the specified element is visible on the page. |
presenceOfAllElementsLocatedBy(By locator) |
Waits until all elements specified by the locator are present in the DOM. |
textToBePresentInElementValue(By locator, String text) |
Waits until the specified text is present in the value attribute of the element. |
frameToBeAvailableAndSwitchToIt(By locator) |
Waits until the frame is available and switches to it. |
invisibilityOfElementLocated(By locator) |
Waits until the element is either not present in the DOM or not visible on the page. |
elementToBeClickable(By locator) |
Waits until the element is visible and enabled, so it can be clicked. |
stalenessOf(WebElement element) |
Waits until the specified element is no longer attached to the DOM. |
elementToBeSelected(By locator) |
Waits until the element is selected. |
elementSelectionStateToBe(By locator, boolean selected) |
Waits until the element's selection state matches the specified value. |
elementLocatedToBeSelected(By locator) |
Waits until the element located by the locator is selected. |
alertIsPresent() |
Waits until an alert is present on the page. |
Fluent Wait
Fluent wait is the most advanced form of explicit wait. It allows you to define:
1. Time to wait
2. Polling Frequency (How often the condition is to be checked)
3. The exception will be ignored while waiting
Example for Fluent Wait:
This code checks for the element every two seconds, for up to 15 seconds, and ignores noSuchElementException during these 15 seconds.
How to Select Appropriate Wait and When to Use It
Implicit Wait: It is a global timeout applied to the WebDriver instance. It instructs Selenium to wait for a specified duration when trying to locate elements before throwing a NoSuchElementException. It affects all element searches performed using findElement() and findElements().
Suitable for handling minor delays in element availability across the entire script. A short timeout, like 3 seconds, is usually sufficient.
Explicit Wait: It is used to wait for a specific condition to be met before proceeding further in the code. It is applied to individual elements
Certain elements load slower than others, and sometimes we need to wait for specific conditions before performing an action or assertion. In such cases, explicit wait can be used.
Fluent Wait: It is a customizable form of explicit wait. When elements behave inconsistently, fluent wait can be used to avoid flakiness and perform fine-tuning.