selenium-webdriver Different types of explicit wait conditions


Example

In explicit wait, you expect for a condition to happen. For example you want to wait until an element is clickable.

Here is a demonstration of a few common problems.

Please note: In all of these examples you can use any By as a locator, such as classname, xpath, link text, tag name or cssSelector


Wait until element is visible

For example, if your website takes some time to load, you can wait until the page completes loading, and your element is visible to the WebDriver.

C#

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("element-id")));

Java

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("element-id")));

Wait until element is not visible anymore

Same as before, but reversed.

C#

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.Id("element-id")));

Java

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("element-id")));

Wait until text is present in the specified element

C#

IWebElement element = driver.FindElement(By.Id("element-id"));

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.TextToBePresentInElement(element, "text"));

Java

WebElement element = driver.findElement(By.id("element-id"));

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.textToBePresentInElement(element, "text"));

If you go to the given link above, you will see all the wait condition there.

The difference between the usage of these wait conditions are in their input parameter.

That means you need to pass the WebElement if its input parameter is WebElement, you need to pass the element locator if it takes the By locator as its input parameter.

Choose wisely what kind of wait condition you want to use.