What does explicit wait perform?


Explicit waits are applied to a specific element in the web page. It shall pause the execution till the condition is satisfied. Explicit wait is also a dynamic one since if the wait time is fifteen seconds and the conditions (like waiting for an element to be clickable, visible or selectable and so on) are satisfied before this specified time, the control will move to the next step.

Explicit wait is more customizable since we can set it up for the condition. The listof some of the expected conditions for explicit waits are listed below −

  • textToBePresentInElement()

    Syntax

    w.until(ExpectedConditions.textToBePresentInElement(By.id(“<<id expression>>“), “Tutorialspoint”));
  • textToBeClickable()

    Syntax

    w.until(ExpectedConditions.textToBeClickable(By.id(“<<id expression>>“)));
  • alertisPresent()

    Syntax

    w.until(ExpectedConditions.alertisPresent())= null);
  • frameToBeAvailableAndSwitchToIt()

    Syntax

    w.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id(“<<frame id >>“)));

Explicit is complex in terms of implementation, however it does not affect the speed of execution and applies to a specific element on a page.

In explicit wait, once the maximum time has passed, ElementNotVisibleException is thrown.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.Wait;
public class Explictwt {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      //implicit wait with time in seconds applied to each elements
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      //Clicking on Coding Ground link
      driver.findElement(By.xpath("//span[text()=’Coding Ground’]")).click();
      // explicit wait declaration
      WebDriverWait w = new WebDriverWait(driver,10);
      // condition to wait for with textToBePresentInElement method
      w.until(ExpectedConditions.textToBePresentInElement(By.xpath("//img[@title=’Whiteboard’]"),”          Whiteboard”));
      driver.quit();
   }
}

Updated on: 10-Jun-2020

350 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements