Using Implicit and Explicit Wait in Selenium

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:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)

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:

  1. Applied globally and hence affects all elements
  2. It can be only used to find elements and not conditions
  3. The polling interval cannot be changed
  4. 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:

  • alertIsPresent()
  • elementSelectionStateToBe()
  • elementToBeClickable()
  • elementToBeSelected()
  • frameToBeAvaliableAndSwitchToIt()
  • invisibilityOfTheElementLocated()
  • invisibilityOfElementWithText()
  • presenceOfAllElementsLocatedBy()
  • presenceOfElementLocated()
  • textToBePresentInElement()
  • textToBePresentInElementLocated()
  • textToBePresentInElementValue()
  • titleIs()
  • titleContains()
  • visibilityOf()
  • visibilityOfAllElements()
  • visibilityOfAllElementsLocatedBy()
  • visibilityOfElementLocated()

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:

Explicit:

  1. Is in part of selenium which is local
  2. Can handle most of the conditions
  3. Returns either success or timeout error
  4. A success condition can even be the absence of an element
  5. Polling time can be customized
  6. But the as long and complicated syntax

Implicit:

  1. Simple usage
  2. Is in the part of Selenium that is Global (the part controlling the browser)
  3. Can only work with finding elements
  4. Returns element found or error
  5. Polling time cannot be customized

Need help? Contact us now!