Equivalent of waitForVisible/waitForElementPresent in Selenium WebDriver tests using Java?


There are equivalent methods for waitForVisible/waitForElementPresent in Selenium webdriver. They are a part of the synchronization concept in Selenium.

The implicit and explicit waits are the two types of waits in synchronization.

The implicit wait is the wait applied to the webdriver for a specified amount of time for all elements. Once this time has elapsed and an element is still not available, an expectation is thrown.

Syntax

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

Example

Code Implementation with implicit wait

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class ImplctWait{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
         "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      //implicit wait of 5 secs
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://www.tutorialspoint.com/index.htm");
      // identify element
      WebElement m = driver.findElement(By.tagName("h4"));
      System.out.println("Element text is: " + m.getText());
      driver.quit();
   }
}

Output

The explicit wait is the wait applied to the webdriver for a specified amount of time for an expected condition to be satisfied for an element. Once this time has elapsed and the expected criteria is not satisfied, an exception is thrown.

An equivalent to waitForVisible method can be the visibilityOfElementLocated expected condition in explicit wait. Also, an equivalent to waitForElementPresent method can be the presenceOfElementLocated expected condition.

Syntax

WebDriverWait w= (new WebDriverWait(driver,5 ));
w.until(ExpectedConditions.presenceOfElementLocated(By.id("txt")));
w.until(ExpectedConditions.visibilityOfElementLocated (By.name("nam-txt")));

Example

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;
public class ExplicitWt{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
         "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      //launch URL
      driver.get("https://www.tutorialspoint.com/index.htm");
      //explicit wait condition - presenceOfElementLocated
      WebDriverWait w= (new WebDriverWait(driver, 7));
      w.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Library']")));
      WebElement m = driver.findElement(By.xpath("//*[text()='Library']"));
      m.click();
      //explicit wait condition - visibilityOfElementLocated
      w.until(ExpectedConditions.visibilityOfElementLocated (By.linkText("Subscribe to Premium")));
      WebElement n = driver.findElement(By.linkText("Subscribe to Premium"));
      String s = n.getText();
      System.out.println("Text is: " + s);
      driver.quit();
   }
}

Output

Updated on: 07-Apr-2021

493 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements