WebDriverWait In Selenium | Selenium WebDriver Tutorial
Selenium WebDriverWait is one of the Explicit waits.
Explicit waits are confined to a particular web element. Explicit Wait is code you define to wait for a certain condition to occur before proceeding further in the code.
Explicit wait is of two types:
- WebDriverWait
- FluentWait
Click on this link for FluentWait.
WebDriverWait In Selenium:
It is applied on certain element with defined expected condition and time. This wait is only applied to the specified element. This wait can also throw exception when element is not found.
We could avoid throwing exception in Selenium. Check this post.
The following are the Expected Conditions that can be used in Explicit Wait
- alertIsPresent()
- elementSelectionStateToBe()
- elementToBeClickable()
- elementToBeSelected()
- frameToBeAvaliableAndSwitchToIt()
- invisibilityOfTheElementLocated()
- invisibilityOfElementWithText()
- presenceOfAllElementsLocatedBy()
- presenceOfElementLocated()
- textToBePresentInElement()
- textToBePresentInElementLocated()
- textToBePresentInElementValue()
- titleIs()
- titleContains()
- visibilityOf()
- visibilityOfAllElements()
- visibilityOfAllElementsLocatedBy()
- visibilityOfElementLocated()
Must Read: Waits in Selenium – Implicit, Explicit, FluentWait
Syntax:
//WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut); WebDriverWait wait = new WebDriverWait (driver, 20); wait.until(ExpectedConditions.VisibilityofElementLocated(By.xpath(""//button[@value='Save Changes']"")));
Test script with an explanation:
Find the sample script (using Java) mentioned below. Execute it to see the functionality.
package waits; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class ExplicitWaits { public static void main(String[] args) { //To create a new instance of Firefox Driver WebDriver driver = new FirefoxDriver(); //To open a website "Software Testing Material" driver.get("https://www.softwaretestingmaterial.com"); //To maximize the browser window driver.manage().window().maximize(); //This waits up to 15 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 15 seconds WebDriverWait wait = new WebDriverWait (driver, 15); //Title of the webpage is "Software Testing Material - A site for Software Testers" wait.until(ExpectedConditions.titleIs("Software Testing Material - A site for Software Testers")); //If the above condition met then the browser will be closed //To close the browser driver.close(); //Change the title "Software Testing Material - A site for Software Testers" as "xyz" in the script and try //You will face an execption - Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 20 seconds waiting for title to be "Software Testing Material - A site for Software Tes". Current title: "xyz" } }
The default pooling period for implicit and explicit wait is 250 ms. Here in Fluent wait, we could change the default pooling period based on our requirement. Also we could ignore any exception while pooling element such as No Such Element exception.
If you are not regular reader of my blog then I highly recommend you to sign up for the free email newsletter using the below link.