Different Types Of Wait in Selenium Web Driver

Published On: 7 April 2016.By .
  • Quality Engineering

Sometime our test fails due to page was not loaded with all element because of slow internet connection or may be the poor response time. So now question is how to handle this problems.? And when to use which wait in selenium.?

There is 2 different way to handle this using waits:

1. Implicit Waits
2. Explicit Waits

1. Implicit Waits

WebDriver waits for an element if they are not immediately available. So, WebDriver does not throw NoSuchElementException immediately. This is known as implicitlyWait(). This can be achieved using:

Important Note:
1. If the DOM is get loaded within the wait time it should not wait for the remaining time it go for the next step,
for an example:

Here we wait for 20 seconds, after that it gives NoSuchElementException. If the element present in 5 second then it should not wait for another 15 seconds.

Disadvantages:
(i) In any case, it blindly wait for given seconds.
(ii) Once set, the implicit wait is set for the life of the WebDriver object instance.

big-logo

2. Explicit Waits

A. Thread.sleep()
This is to wait the running program for sometime, this can be done using:

Ex.- Thread.sleep(3600).

Disadvantages:
(i) In this case, it again blindly wait for given seconds. It is not a better way because if the the element is present within this given wait time, program does not move further until the wait time finished.
(ii) Some computers are slow, an element may not show up in a slow computer within the given wait time.
3.It is sleep time for script, not good way to use in script as it sleeps without condition.

B. Expected Condition
1. An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code. There are some convenient methods provided that help you to write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

Ex.- WebDriverWait wait = new WebDriverWait(driver, 15);

WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id()));

This waits up to 15 seconds before throwing a TimeoutException or if it finds the element will return it in 0 – 15 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

Note : This example is also functionally equivalent to the first Implicit Wait example.

2. There are some common conditions that are frequently come across when automating web browsers. Listed below are Implementations of each. Java happens to have convienence methods so you don’t have to code an ExpectedCondition class yourself or create your own utility package for them.

– Element is Clickable – it is Displayed and Enabled.

Note : The ExpectedConditions package (Java), (Python), (.NET) contains a set of predefined conditions to use with WebDriverWait.

3. Fluent Wait Command: Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as ‘NoSuchElementExceptions’ when searching for an element on the page.

WARNING: (i) Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.
(ii) Never ever use both implicit wait and explicit wait on same driver. Its not good practice. Implicit wait is implemented at driver level where as Explicit wait is implemented at language binding level (e.g. Java, c# etc.)

PageLoadTimeout Command:
Purpose: Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

SetScriptTimeout Command:
Purpose: Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.

Related content

That’s all for this blog

Go to Top