What are the features of Explicit wait in Selenium?

Can anyone please inform me about the eatures of Explicit wait in Selenium.

The Explicit wait is one of the dynamic Selenium waits. Explicit wait help to stop the execution of the script based on a certain condition for a specified amount of time. Once the time goes overboard, you will get the ElementNotVisibleException. In a scenario where you do not know the amount of time to wait for, this explicit wait comes in handy. Using conditions like elementToBeClickable() or textToBePresentInElement(), one can wait for the specified duration. One can use these predefined methods using the combination of classes WebDriverWait and ExpectedConditions. In order to use this case, import the below packages in your class:

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

Post this one needs to create a reference variable for the WebDriverWait class and instantiate it using the WebDriver instance and providing the amount of Selenium wait for page load, one may need. The unit of time is in seconds. One can define it as below:

WebDriverWait wait = new WebDriverWait(driver,30);

In order to use the predefined methods of the ExpectedCondition Class, we will use the wait reference variable as below:

wait.until(ExpectedConditions.visibilityOfElementLocated(Reference of Element to be located using locator));

Types of Expected Conditions:

Below are the few types of expected conditions commonly used as you perform automation testing with Selenium.

  • visibilityOfElementLocated()- Verifies if the given element is present or not

  • alertIsPresent()- Verifies if the alert is present or not.

  • elementToBeClickable()- Verifies if the given element is present/clickable on the screen

  • textToBePresentInElement()- Verifies the given element have the required text or not

  • titlels()- Verify the condition wait for a page that has a given title

There are many more expected conditions available, which you can refer through the Selenium official GitHub page. Like Implicit wait, the explicit wait also keeps polling after every 500 milliseconds.

Below is the code snippet highlighting the usage of an Explicit Selenium wait. In this example, we are using the ‘rentomojo’ application, where a modal appears at a dynamic time on the homepage. Using explicit wait, based on the element visibility, we will wait for the element and close the pop-up.

Referenced code:

import java.util.concurrent.TimeUnit;
import http://org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ExplicitWait {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //setting the driver executable
                System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");
                //Initiating your chromedriver
                WebDriver driver=new ChromeDriver();
                driver.manage().window().maximize();
                driver.get("Start Renting | Furniture, Appliances, Electronics");
                driver.findElement(By.xpath("//span[@class='rm-city__selectorBoxLoca'][contains(text(),'Delhi')]")).
                WebDriverWait wait=new WebDriverWait(driver, 120);     wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[@class='Campaign__innerWrapper']/button"))));         driver.findElement(By.xpath("//div[@class='Campaign__innerWrapper']/button")).click();
    }
}

Note- When implicit wait and explicit wait are given in conjunction, then they work on cumulative time, rather than on a single wait condition. For example, if the Implicit wait is given for 30 seconds and the Explicit wait is given for 10 seconds, then the explicit element it is looking for will wait for 40 seconds.

Features of Explicit Wait:

Explicit wait in Selenium has a number of key features (or differences) than other types of Selenium waits:

  • Unlike implicit wait, the explicit wait command in Selenium is documented and has a defined behavior.
  • Explicit wait executes on the local part of Selenium i.e. the programming language of your code, whereas implicit wait works on the remote part of Selenium i.e. the one controlling the web browser.
  • It can work with any possible condition (e.g. elementToBeClickable, alertIsPresent, invisibilityOfElementWithText, , stalenessOf, etc.) unlike implicit wait that only works on findelement methods.
  • Explicit wait in Selenium can also be used in case you are checking for the absence of a web element on the page.
  • The delay between retries can be customized using adjusting the .pollingInterval property which is by default set to 250 ms. On the other hand, delay in implicit waits can only be customized through the global timeout.