In Previous tutorial, we have discussed on the structure dependency in Locating strategy. Now we will discuss how to implement waits in Selenium. Before going into details on the implementation of waits in selenium, let’s discuss some basic knowledge on Waits.
What is Waits?
Now a days most of the application are developed in advanced technology like Ajax, JavaScript and many more. This can result the page loading slow. Also every script will take different time to load the page. As a result your page loading time will be different for different page.
In this scenario, if you want to handle the locator in page in selenium, you may get “ElementNotVisibleException” exception because of page loading time. If page is not loaded properly, you may not get the proper element which you want to search through xpath or any other locator strategy.
To overcome this troubleshoot we will implement waits in Selenium so that script will wait for specified time and run after waits completes.
There are 3 types of waits present in Selenium.
- Implicit
- Explicit
- Fluent

Let’s discuss one by one.
Implicit Waits:
Out of 3 waits which are listed above, implicit wait is the default wait in Selenium. This is the default waiting time between the test step before throwing “No Such Element Exception” Exception. Once you have defined the implicit wait code in the script, it will valid for the entire script. You do not need to define again and again in each of the line when required.
That means, once you add the implicit code then this will be implement before executing each line of the code.
Syntax:
driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
To work with implicit wait we need to import below packages.
import java.util.concurrent.TimeUnit;
Let’s discuss the example .
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class Implicitwaittesting { WebDriver driver; @Test public void Implicitwait() throws InterruptedException { driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS) ; String pageTitle = "TestingLpoint"; String actualTitle = "" ; driver.get("https://www.testinglpoint.com"); driver.manage().window().maximize() ; actualTitle = driver.getTitle() if (actualTitle.equals(pageTitle)) { System.out.println( "Test Passed") ; } else { System.out.println( "Test Failed" ); } driver.close(); } }
Explanation:
From the above code you can notice that we have used the implicitlyWait() only once after defining the browser driver. implicitlyWait() accepts only 2 argument i.e.
- Wait time – Integer
- Unit of the Wait time – TimeUnit.SECONDS
Wait time is the integer which described as the waiting time between the test step before throwing “No Such Element Exception” Exception. In this above example we have provided the wait time as 20.
Second argument specifies the unit of the wait time we have given in the first argument. The syntax of the second argument is TimeUnit.SECONDS. That means script will wait for 20 second before executing each line of the script. If you want to implement the wait time as 1 min, then you can give 60 in 1st argument and TimeUnit.SECONDS in 2nd argument or 1 in 1st argument and TimeUnit.MINUTE in 2nd argument.
Explicit Wait:
Explicit wait is the same as implicit wait with some advance feature. This indicates the WebDriver to pause the executing the test step until some expected condition or maximum time exceeded before throwing the “ElementNotVisibleException” Exception.
Unlike Implicit wait, Explicit wait is used for applying wait time on the specific element. When we define the explicit wait in our automation script, it will wait for the expected condition to be satisfied.
You can ask that why explicit wait is used when we have Thread.sleep(). The main reason is thread.sleep is always wait for the specified time where as explicit wait waits for a certain condition to be met.
Also thread.sleep is not recommended to use because it delayed the test case execution without verifying any conditions.
Syntax:
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut); WebElement Testinglpointexplicitwait = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id( "live"))); Testinglpointexplicitwait.click(); }
From the above syntax you can notice that wait.until() will return the web element for which expcted condition should be verified. IN the b=above code we are verifying whether the element ehose id is live is visible or not. So out script will wait until this element is visible.
Addition to these you can notice the first line of the syntax. We have defined the WebDriverWait class to define the maximum time the script should wait.
If script does not locate the element until the maximum time mentioned in WebDriverWait class then this will terminate the run and proceed to the next step.
Like visibilityOfElementLocated we have defined in the above script, ther are lot of expected condition is available in selenium wait. i.e.
In Next tutorial we will discuss on Fluent wait.