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);
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();
}
}
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();
}
}
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]"))));
}
}
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]"))));
}
}
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")));
}
}
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]")));
}
}
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.
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
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.