Explicit Implicit wait selenium is an important command. They are used while running automation scripts created using Selenium Web Driver. Implicit and Explicit Wait in Selenium is primarily used to handle the different load times of elements on the web browser.
Using Implicit and Explicit Wait in Selenium Example
Consider a situation in which you are testing the Facebook web application and you want to test a scenario in which you post a comment. When you automate this test scenario, there is a possibility it may fail, especially during low bandwidth and high usage. The reason for the automation failure, in such cases, is that the post button was yet to load. Elements on a web application load at different times.
In order to handle this, Selenium has introduced the usage of “Explicit” and “Implicit” wait. Selenium documentation on Explicit and Implicit wait warns users not to use both together.
Using Implicit Wait in Selenium
The implicit wait is a quick and easy way to handle the problem of elements loading at different times on a web application. It is applicable at a global level and affects all the elements. It basically tells selenium to wait for a specified time before it throws a “No Such Element” Exception.
Let’s dive into an Example:
In the above example, we have instructed selenium to wait for the time frame of 10 seconds if the element is not present.
It means that if the element is not located on the web page within that time frame, it will throw an exception. The script will keep polling the DOM to find if the element has shown up. The polling interval cannot be changed. If the element is found selenium continues with the execution.
There are some apparent drawbacks to this:
- Applied globally and hence affects all elements
- It can be only used to find elements and not conditions
- The polling interval cannot be changed
- It will slow down the execution time
Using Explicit Wait in Selenium
The explicit wait is a remedy to some of the problems which occur in implicit wait. However, the flip side to explicit wait is the complexity and the number of lines of code. When we use Explicit wait, we tell the Selenium web driver to wait for a condition to occur. This is different from waiting for a specified period of time.
Here is an example:
WebDriverWait wait = new WebDriverWait(<expected condition>,10)
Here selenium will wait for an expected condition or 10 seconds whichever is earlier. The list of expected conditions for Java can be found here. Some of these conditions are listed below:
A user on Trello explained the differences in using Explicit and Implicit very well. I am summarizing the differences between Implicit and Explicit Wait selenium here:
- Is in part of selenium which is local
- Can handle most of the conditions
- Returns either success or timeout error
- A success condition can even be the absence of an element
- Polling time can be customized
- But the as long and complicated syntax
- Simple usage
- Is in the part of Selenium that is Global (the part controlling the browser)
- Can only work with finding elements
- Returns element found or error
- Polling time cannot be customized
Need help? Contact us now!