Join us on :

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. Explicit wait will be applicable for only one line (one conditon), 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 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 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 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 some text is not present in he webpage
      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 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 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 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?
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

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • poorna pragna
    Excellent one!
    
    However,  small hiccups in all the 'Expected Conditions' expect 'alertPresent' rest others wrongly commented it as
    //throws TimeoutException if no alert is present.
    
    Another one is this  wait.until(ExpectedConditions.wait.until(ExpectedConditions.visibilityOfElementLocated....
    added  extra as 'ExpectedConditions.wait.until' twice pls explain it.
    
    Thanks, Sir!
    Poorna
    
    Reply
    • admin[Karthiq]
      Thanks for appreciating and pointing our errors.
      
      I have started to make all changes that you are suggesting.
      
      1. I have corrected the comments
      2. 'until(ExpectedConditions.wait.until(ExpectedConditions" it happened by mistake. This one is also corrected now.
      
      
      Reply