What is Explicit Wait in Selenium

In this article, we learn what is explicit wait in selenium, the syntax of explicit wait, an example of explicit wait, and expected conditions.

What is Explicit Wait in Selenium?

Selenium WebDriver provides a special kind of dynamic and customization wait called Explicit wait.

Explicit Wait in selenium is also known as Dynamic Waits because it is highly expressly conditioned. It is implemented by WebDriverWait class.

Setting Explicit Wait is important if you have certain elements that naturally take a long time to load. Setting the implicit wait command causes the browser to wait for the same timeframe before loading all her web elements. This causes an unnecessary delay in running the test script.

Explicit wait in selenium is more intelligent but only applicable to the specified element. However, it improves implicit waits because the program can pause dynamically loaded Ajax elements.

The explicit wait can be used to wait until our desired condition or conditions are met or it runs out of the maximum defined wait time. Write for a particular WebElement for the particular condition.

These days, we use Thread.Sleep() in implementations, but we wouldn’t recommend using it in general.

Condition 1:

Suppose you have a web page consisting of a login form that takes input and loads the content of the home page or main page. This page is dynamic due to time constraints and network frequency and may take 10 or even 15 seconds to fully load. Explicit waits are useful in cases like this, allowing you to wait until the page is no longer displayed.

Condition 2:

Suppose we are working on a travel application and a user fills out her web form and submits using the submit button. Now you may have to wait until certain data is no longer displayed. Explicit Wait can help in such cases by waiting for a certain period of time for a set of elements that have not yet been seen.

Syntax of Explicit Wait in Selenium

WebDriverWait wt=new WebDriverWait (driver, Duration.ofSeconds(5))

.until (ExpectedConditions.elementToBeClickable(driver. findElement (By.id (“_”)))). Click ();

The above syntax justifies an object of WebDriver Wait, passed to the driver’s preference, and takes the timeout as a parameter.

Example of Explicit Wait in Selenium

To use Explicit Wait in test scripts, import the following packages into the script.

import org.openqa.selenium.support.ui.ExpectedConditions

import org.openqa.selenium.support.ui.WebDriverWait

We Initialize A Wait Object using WebDriverWait Class.

WebDriverWait wait = new WebDriverWait(driver,10);

Here, the reference variable is named wait for the WebDriverWait class. It is instantiated using the WebDriver instance. The maximum wait time must be set for the execution to lay off. Note that the wait time is measured in seconds.

Example of Explicit Wait in Selenium

When the above program is executed, the Chrome driver will launch Chrome, and it will navigate through https://google.com/ncr to take the mentioned values. It is, although not mandatory, to explicitly set the timeout for a particular value since it is changeable. Another advantage to adding up here is that once you define the timeout for 10 seconds, it becomes applicable for all the elements present on the web page, and then it cannot be modified. This is the best way to execute, understand and execute explicit wait in selenium.

Below are the expected conditions that can be used with Explicit Wait in Selenium

  1. alertIsPresent()
  2. elementSelectionStateToBe()
  3. elementToBeClickable()
  4. elementToBeSelected()
  5. frameToBeAvaliableAndSwitchToIt()
  6. invisibilityOfTheElementLocated()
  7. invisibilityOfElementWithText()
  8. presenceOfAllElementsLocatedBy()
  9. presenceOfElementLocated()
  10. textToBePresentInElement()
  11. textToBePresentInElementLocated()
  12. textToBePresentInElementValue()
  13. titleIs()
  14. titleContains()
  15. visibilityOf()
  16. visibilityOfAllElements()
  17. visibilityOfAllElementsLocatedBy()
  18. visibilityOfElementLocated()

Other Popular Articles

Implicit, Explicit And Fluent Waits In Selenium

What is Difference Between Git and Github

Leave a Comment