Hello People, As you may be knowing If you want to become an expert at using Selenium Webdriver, one of the most important skills to master is the use of the Wait commands. Waits are essential for executing test scripts and help identify and resolve issues related to the time lag in web elements. This article will offer a detailed description of how testers can use the Wait function in Selenium. This article will provide clarity about Fluent Wait on when to use which function.
What are Wait Commands In Selenium?
The wait commands are essential when it comes to executing Selenium tests. They help to observe and troubleshoot issues that may occur due to variations in time lag.
While running Selenium tests, it is common for testers to get the message “Element Not Visible Exception“. This appears when a particular web element with which WebDriver has to interact, delays in its loading. To prevent this Exception, Selenium Wait Commands should be used.
In automation testing, wait for commands direct test execution to pause for a certain time before moving on to the next step. This enables WebDriver to check if one or more web elements are present/visible/enriched/clickable, etc.
Why do users need Selenium Wait commands?
When a web page loads on a browser, various web elements (buttons, links, images) that someone wants to interact with may load at multiple intervals.
Selenium WebDriver provides three commands to implement waits in tests.
- Implicit Wait
- Explicit Wait
- Fluent Wait

Fluent Wait in Selenium
- In Selenium Fluent wait makes it possible by marking the maximum amount of time for Selenium WebDriver to wait for a certain condition (web element) to become visible. It also defines how frequently WebDriver will check if the condition appears before throwing the “ElementNotVisibleException”.
- This Wait is quite similar to explicit Wait. It is similar in terms of management and functioning. In this Wait, you can perform wait for action for an element only when you are unaware of the time it might take to be clickable or visible.
- Referring to Fluent waits as smart waits. They are called smart primarily because they don’t wait for the max time out, specified in the .withTimeout(5000, TimeUnit.MILLISECONDS). Instead, it waits for the time till the condition specified in the .until(YourCondition) method becomes true.
Syntax
Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);
WebElement foo=wait.until(new Function<WebDriver, WebElement>() {
public WebElement applyy(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});
Although the syntax appears to be very complex, it comes in handy when you start using it. This is why probably all the software testers prefer using Explicit and Implicit Wait because of the simple syntax. Fluent waits appear complex because they don’t provide predefined conditions that need to apply to the elements. Rather, Fluent Wait defines its condition within the apply method.
Differential Features Of Fluent Wait
The Fluent wait is similar to an explicit wait in terms of its functioning. In Fluent wait, you perform a Selenium wait for an element when you are not aware of the time it may take to be visible or clickable. The few differential factors that Fluent wait offers are:
- The polling frequency- In the case of Explicit wait, this polling frequency is by default 500 milliseconds. Using Fluent wait, you can change this polling frequency based on your needs, i.e you can tell your script to keep checking on an element after every ‘x’ seconds.
- Ignore Exception- During polling, in case you do not find an element, you can ignore any exception like the ‘NoSuchElement’ exception, etc.
Apart from these differential factors, like Explicit wait or Implicit wait, you can define the amount of time to wait for the element to be visible or actionable.
Script:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import com.google.common.base.Function;
public class FluentWaitExp {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"E:\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.name("q")).sendKeys("Selenium");
Thread.sleep(2000);
driver.findElement(By.name("btnK")).click();
@SuppressWarnings({ "deprecation" })
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30,
TimeUnit.SECONDS)
.pollingEvery(2, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
WebElement element = (WebElement) wait.until(new Function<WebDriver,
WebElement>() {
@Override
public WebElement apply(WebDriver arg0) {
WebElement linkelement = driver.findElement(By.partialLinkText("Selenium -
Health Professional"));
if (linkelement.isEnabled()) {
System.out.println("Element is Found");
}
return linkelement;
}
});
element.click();
}
}
Why Using Thread.Sleep() Is Not A Good Idea?
I will now be highlighting some disadvantages of using the thread. sleep().
- Selenium Webdriver waits for the specified time, irrespective of whether the element is found or not. In case the element is found much before the specified duration, the script will still wait for the time duration to elapse, thereby increasing the execution time of the script.
- If the element to be present does not appear after a static time and keeps changing, then you will never know an estimated time needed for the sleep function. In case it takes more time than the defined time, the script shall throw an error. This is why, if you are dealing with dynamic elements using Selenium waits then it is wise to not use Thread. sleep().
- Thread. sleep only intends for the element which we write before. In case you have two to four elements that need to wait for a certain duration to load, you need to specify Thread. sleep as many times in that case. And if you do that! Well, you will find your script filled with Thread. sleep() syntax is all over the place.
Owing to the above disadvantages, using Thread.Sleep() in your script creation is considered a bad practice.
The Final Verdict On Selenium Waits!
Selenium Waits helps to make your scripts less flaky and more reliable. Whichever wait you choose to go with, make sure it does the business to help you achieve your purpose behind automation testing with Selenium. Another key thing to note is to ensure not to keep unnecessary Selenium waits in your application. Thread. sleep() may have been an option in the past, but now that we have new Selenium waits up & coming, I would rather recommend you to drive your Selenium automation testing away from Thread. sleep(). Happy testing!
For more Reference Visit:
https://www.selenium.dev/documentation/webdriver/


