Best practice to wait for a change with Selenium Webdriver?


The best practice to wait for a change in Selenium is to use the synchronization concept. The implicit and explicit waits can be used to handle a wait. The implicit is a global wait applied to every element on the page.

The default value of implicit wait is 0. Also it is a dynamic wait, meaning if there is an implicit wait of 5 seconds and the element becomes available at the 3rd second, then the next step is executed immediately without waiting for the entire 5 seconds. Once the 5 seconds have elapsed, and if element is not found, a timeout error shall be thrown.

Syntax

driver.manage().timeouts().implicitlyWait();

Example

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 ImpctWt {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // wait of 5 seconds
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // findElement() will try to identify element till 5 secs
      WebElement n=driver.findElement(By.id("gsc−i−id1"));
      n.sendKeys("Java");
   }
}

The explicit wait is also used and it is applied to a specific element on the page. It is a WebDriverWait that works in association with the Expected Condition class. It is a dynamic wait, meaning if there is an explicit wait of 5 seconds and the element becomes available at the 3rd second, then the next step is executed immediately. Once the 5 seconds have elapsed, and element is not found, timeout error shall be thrown.

Some of the expected conditions for explicit waits are −

  • titleContains(String s)

  • alertIsPresent()

  • invisibilityOfElementLocated(By locator)

  • invisibilityOfElementWithText(By locator, String s)

  • textToBePresentInElement(By locator, String t)

  • visibilityOfElementLocated(By locator)

  • presenceOfAllElementsLocatedBy(By locator)

  • visibilityOf(WebElement e)

  • presenceOfElementLocated(By locator)

  • elementToBeClickable(By locator)

  • stalenessOf(WebElement e)

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 ExpltWt {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.get("https://www.tutorialspoint.com/index.htm");
      // identify element and click()
      driver.findElement(By.xpath("//*[text()='Library']")).click();
      // expected condition − invisibility condition
      WebDriverWait wt = new WebDriverWait(driver,5);
      // invisibilityOfElementLocated condition
      wt.until(ExpectedConditions.
      invisibilityOfElementLocated(By.xpath("//*[@class='mui−btn']")));
      driver.close();
   }
}

Updated on: 28-Nov-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements