Explicit Wait in Selenium WebDriver – Java (Synchronization)

  • Explicit waits are used to halt script execution untill a particular condition is met or the maximum time has elapsed. Unlike Implicit waits, Explicit waits are applied to a particular web element only.
  • WebDriver will wait untill element became visible (visibilityOfElementLocated(By.xpath(“Enter xpath”)) on the basis of time provided in wait condition (WebDriverWait(driver, Duration.ofSeconds(20))) . However, if web driver is unable to find an element in a given time, it will throw “ElementNotVisibleException“.
  • Web driver will keep on looking to DOM (Document Object Model) to check if the element is present or not. In case, web driver finds the web element before the time provided in explicit wait, it will exit the wait and starts the execution.

 Must Read : Handle ElementNotVisibleException in Selenium Webdriver

                       Implicit Wait in Selenium WebDriver – Java (Synchronization)

Syntax:

// Create instance of WebDriverWait class 
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
  
// Wait till the element becomes visible
 WebElement webElement=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("Enter xpath")));

Code for Explicit wait:

  • In below code, we are waiting for an element for the time frame of 20 seconds as defined in the “WebDriverWait” class until the “ExpectedConditions.visibilityOfElementLocated()” is met. In other words, web driver will wait for 20 seconds before throwing an exception.
  • However, web driver will keep on looking to DOM (Document Object Model) to check if the element becomes visible or not. In case, web driver finds the web element before 20 seconds, it will exit the wait. Suppose web driver finds the web element in 5 seconds, then web driver will exit the explicit wait condition after 5 seconds and will starts executing the next steps of script.
package TestCases;

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class ExplicitWaitTest {
 public WebDriver driver;

 @Test
 public void explicitWait() {
  // Create instance of Chrome Driver
  System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
  driver = new ChromeDriver();

  // Navigate to particular Website
  driver.get("https://makeinjava.com");

  // Create instance of WebDriverWait class, We are giving wait time of 20 seconds
  WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));

  // Web driver will wait until element became visible
  // Below method will return element if element became visible 
  // If element not found, it will throw an exception
  WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("Enter Xpath here")));

  if (element.isDisplayed()) {
   System.out.println("Element is visible=");
  } else {
   System.out.println("Element is not visible");
  }

 }
}

Expected Conditions that can be used in Explicit Wait:

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