Explicit Implicit wait selenium is important commands. They are used while running automation scripts created using Selenium Web Driver. Explicit Implicit wait in selenium is primarily used to handle the different load time of elements on the web browser.
Using Explicit Implicit Wait Selenium – A Practical Scenario
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 because 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:
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. During this time, 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 clear drawbacks to this:
- Applied globally and hence effects all elements
- Can be only used to find elements and not conditions
- Polling interval cannot be changed
- It will slow down the execution time
Using Explicit Wait in Selenium
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 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 those differences of Explicit Implicit 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 absence of an element
- Polling time can be customized
- But has long and complicated syntax
- Simple usage
- Is in the part of selenium which is Global (the part controlling the browser)
- Can only work with finding elements
- Returns element found or error
- Polling time cannot be customized