
🏹 Selenium Waits
Selenium Waits: In Selenium automation testing, Waits are crucial for handling synchronization issues between test execution and the time taken by elements to load or become visible. Without waits, your test scripts may fail due to timing issues like ElementNotVisibleException or NoSuchElementException.
Let’s explore the different types of waits in Selenium and understand how to use them effectively.
📘 Table of Contents
- Why Do We Need Waits in Selenium?
- Types of Waits in Selenium
- What is Implicit Wait in Selenium?
- What is Explicit Wait in Selenium?
- What is Fluent Wait in Selenium?
- When to Use Which Wait?
- Summary
🏹 Why Do We Need Waits in Selenium?
Selenium Waits: Modern web applications often use technologies like AJAX and JavaScript, which load elements asynchronously. Sometimes, the elements you want to interact with are not immediately available when the page loads. Trying to interact with such elements will throw exceptions like:
- ElementNotVisibleException
- NoSuchElementException
To prevent these failures, Selenium provides various wait mechanisms to pause execution until elements become available.
🏹Types of Waits in Selenium WebDriver
Selenium Waits: Selenium supports the following types of waits:
- Implicit Wait
- Explicit Wait
- Fluent Wait
Let’s look into each of them in detail with examples.
🏹 Implicit Wait in Selenium
✅ Definition:
Implicit Wait tells WebDriver to wait for a specified amount of time before throwing an exception if the element is not found immediately.
✅ Syntax:
java
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
✅ Key Points:
- Applies globally across all elements in the script.
- Cannot be targeted to a specific condition.
- Best for stable elements that appear with slight delay.
✅ Example:
java
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get(“https://example.com”);
WebElement element = driver.findElement(By.id(“username”));
If the element is not found in 10 seconds, Selenium will throw a NoSuchElementException.
🏹 Explicit Wait in Selenium
✅ Definition:
Explicit Wait is used to wait for specific conditions to occur before proceeding with the execution.
✅ Syntax:
java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“submit”)));
✅ Key Points:
- Waits for expected conditions like visibility, clickability, etc.
- Scoped wait, used only for specific elements.
- Requires ExpectedConditions class.
✅ Example:
java
WebDriver driver = new ChromeDriver();
driver.get(“https://example.com”);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id(“submit”)));
button.click();
🏹. Fluent Wait in Selenium
✅ Definition:
Selenium Waits: Fluent Wait is an advanced version of Explicit Wait, which lets you define:
- Polling frequency
- Timeout duration
- Exceptions to ignore
✅ Syntax:
java
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(5))
.ignoring(NoSuchElementException.class);
✅ Example:
java
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(20))
.pollingEvery(Duration.ofSeconds(3))
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(driver -> driver.findElement(By.id(“dynamicElement”)));
element.click();
✅ Key Points:
- Best for dynamic elements with unpredictable load time.
- Customizable with retry and polling strategy.
🏹 Combining Implicit and Explicit Waits (Best Practice Warning)
Although you can set both Implicit and Explicit Waits, it’s not recommended to mix them. Combining them may lead to unpredictable wait times and debugging difficulties.
Example: If implicit wait is 20 seconds and explicit wait is 10 seconds, explicit wait may still wait up to 30 seconds due to cumulative delay.
🏹 When to Use Which Wait?
Wait Type | Best Use Case |
Implicit Wait | For elements that appear consistently after delay |
Explicit Wait | For waiting for specific conditions on certain elements |
Fluent Wait | For highly dynamic elements with unknown behavior |
🏹Summary
Here’s a quick recap of the three types of Selenium waits:
- Implicit Wait: Waits for a fixed time for any element globally.
- Explicit Wait: Waits for specific conditions on specific elements.
- Fluent Wait: Waits with a custom polling mechanism and ignores exceptions.
Using the right wait strategy is critical for building reliable, robust, and flake-free test scripts in Selenium automation.