Explicit Wait in selenium webdriver

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.Explicit wait will be applicable for only one line, we have to use it with ExpectedConditions class.

ExplicitWait does not have any effect on findElement and findElements.ExplicitWait also called as 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 user writes 'alertIsPresent()', when selenium finds the alert it moves to next line of code, in case if 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 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 below code wait statement makes selenium to wait for 30 or till the button gets enabled, if button is not enabled in 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 no alert is present
      wait.until(ExpectedConditions.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 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 no alert is present
      wait.until(ExpectedConditions.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 have particular text it moves to next line of code, in case if element doesnot 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 no alert is present
      wait.until(ExpectedConditions.wait.until(ExpectedConditions.textToBePresentInElement(By.xpath("//input[@id='h2]"))));
  }
}

titleIs()

Selenium waits for an webpage to have a particular title when we use 'titleIs()', when selenium finds webpage with given title, it moves to next line of code, in case if webpage doesnot have 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 no alert is present
      wait.until(ExpectedConditions.wait.until(ExpectedConditions.titleIs("selenium webdriver sample")));
  }
}

visibilityOfElementLocated()

Selenium waits for visibility of element when we use 'visibilityOfElementLocated()', when element is visible, it moves to next line of code, in case if 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 no alert is present
    wait.until(ExpectedConditions.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?
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 condition or alerts or titles so on.

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group