Explicit Wait in selenium

The explicit wait is used to tell the Web Driver to wait for certain conditions or the maximum time limit before throwing an Exception .

We can reuse the WebdriverWait object once we create it. The explicit wait will be applicable for only one line (one condition); we have to use it with ExpectedConditions class.

ExplicitWait does not have any effect on findElement and findElements. ExplicitWait also called WebdriverWait.

WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully.

Syntax : WebDriverWait wait=new WebDriverWait( driver, timeoutInSeconds);
Below are few Expected Conditions :
  • alertIsPresent()
  • elementSelectionStateToBe(locator, selected)
  • elementToBeClickable(locator)
  • elementToBeSelected(locator)
  • frameToBeAvailableAndSwitchToIt(locator)-(will see in advanced topic)
  • presenceOfElementLocated(locator)
  • textToBePresentInElement(locator, text)
  • title()
  • titleIs(title)
  • visibilityOf(element)

alertIsPresent()

Selenium waits for an alert to be present when the user writes 'alertIsPresent()', when selenium finds the alert it moves to next line of code, in case if an alert is not present before the specified time, then selenium Throws TimoutException

public class A
{
	public static void main(String[] args) throws InterruptedException
	{
		//open firefox
		WebDriver driver=new FirefoxDriver();
		//open google.com
		driver.get("https://chercher.tech/practice/explicit-wait-sample-selenium-webdriver");
		driver.findElement(BY.xpath("//button[@class='alert']")).click();
		WebDriverWait wait = new WebDriverWait(driver, 30 /*timeout in seconds*/);
		//throws TimeoutException if no alert is present
		wait.until(ExpectedConditions.alertIsPresent());
		driver.switchTo().alert().dismiss();
	}
}

elementToBeClickable()

Selenium waits for an element to become clickable like disabled state to a normal state, selenium moves to next line of code if the element becomes clickable before the timeout otherwise selenium throws TimoutException

Executing below code will fail as the button is disabled

public class A
{
  public static void main(String[] args) throws InterruptedException
  {
      //open firefox
      WebDriver driver=new FirefoxDriver();
      //open google.com
      driver.get("https://chercher.tech/practice/explicit-wait-sample-selenium-webdriver");
      driver.findElement(By.xpath("//button[@id='btn1']")).click();

  }
}


In the below code wait statement makes selenium to wait for 30 or till the button gets enabled; if the button is not enabled in the specified time limit selenium throws 'Timeout Exception.'

public class A
{
  public static void main(String[] args) throws InterruptedException
  {
      //open firefox
      WebDriver driver=new FirefoxDriver();
      //open google.com
      driver.get("https://chercher.tech/practice/explicit-wait-sample-selenium-webdriver");
      WebDriverWait wait = new WebDriverWait(driver, 30 /*timeout in seconds*/);
      //throws TimeoutException if element does not become as clickable in given time
      wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='btn1']"))));
      driver.findElement(By.xpath("//button[@id='btn1']")).click();

  }
}

elementToBeSelected()

Selenium waits for an element to be selected when we use 'elementToBeSelected()', when selenium finds the element is selected it moves to next line of code, in case if the element is not selected before the specified time, then selenium Throws TimoutException

public class A
{
  public static void main(String[] args) throws InterruptedException
  {
      //open firefox
      WebDriver driver=new FirefoxDriver();
      //open google.com
      driver.get("https://chercher.tech/practice/explicit-wait-sample-selenium-webdriver");
      driver.findElement(BY.xpath("//button[@class='alert']")).click();
      WebDriverWait wait = new WebDriverWait(driver, 30 /*timeout in seconds*/);
      //throws TimeoutException if element is not selected in given time
      wait.until(ExpectedConditions.elementToBeSelected(By.xpath("//input[@id='hidden]"))));
  }
}

textToBePresentInElement()

Selenium waits for an element to have particular text when we use 'textToBePresentInElement()', when selenium finds the element has particular text it moves to next line of code, in case if an element does not have text before the specified time, then selenium Throws TimoutException

public class A
{
  public static void main(String[] args) throws InterruptedException
  {
      //open firefox
      WebDriver driver=new FirefoxDriver();
      //open google.com
      driver.get("https://chercher.tech/practice/explicit-wait-sample-selenium-webdriver");
      driver.findElement(BY.xpath("//button[@class='alert']")).click();
      WebDriverWait wait = new WebDriverWait(driver, 30 /*timeout in seconds*/);
      //throws TimeoutException if some text is not present in he webpage
      wait.until(ExpectedConditions.textToBePresentInElement(By.xpath("//input[@id='h2]"))));
  }
}

titleIs()

Selenium waits for a webpage to have a particular title when we use 'titleIs()', when selenium finds a webpage with a given title, it moves to next line of code, in case if the webpage does not have a title before the specified time, then selenium Throws TimoutException

public class A
{
  public static void main(String[] args) throws InterruptedException
  {
      //open firefox
      WebDriver driver=new FirefoxDriver();
      //open google.com
      driver.get("https://chercher.tech/practice/explicit-wait-sample-selenium-webdriver");
      driver.findElement(BY.xpath("//button[@class='alert']")).click();
      WebDriverWait wait = new WebDriverWait(driver, 30 /*timeout in seconds*/);
      //throws TimeoutException if title does not contain give string
      wait.until(ExpectedConditions.titleIs("selenium webdriver sample")));
  }
}

visibilityOfElementLocated()

Selenium waits for visibility of element when we use 'visibilityOfElementLocated()', when the element is visible, it moves to next line of code, in case if the element is not visible before the specified time, then selenium Throws TimoutException

public class A
{
  public static void main(String[] args) throws InterruptedException
  {
    //open firefox
    WebDriver driver=new FirefoxDriver();
    //open google.com
    driver.get("https://chercher.tech/practice/explicit-wait-sample-selenium-webdriver");
    driver.findElement(BY.xpath("//button[@class='alert']")).click();
    WebDriverWait wait = new WebDriverWait(driver, 30 /*timeout in seconds*/);
    //throws TimeoutException if the element is not displayed on the webpage
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='hidden]")));
  }
}

Frequently Asked Questions

What are we waiting for?
Waiting for .until condition is satisfied, until method gets satisfied when it receives neither null nor false.

How long do we wait?
It is defined explicitly in timeout parameter of .withTimeout() method

How often we can check the result?
It is defined in pollingFrequency.

When to use?
When we try to test the presence of an element that may appear after particular seconds/minutes, and many other things like wait till an element’s property satisfy certain conditions or alerts or titles so on.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions