How to add custom ExpectedConditions for Selenium?

SeleniumAutomation TestingTesting Tools

We can add custom ExpectedConditions for Selenium webdriver. We require this custom ExpectedConditions when the default expected conditions provided by webdriver are not enough to satisfy some scenarios.

The method until is used which is a part of the WebDriverWait class. Here, the ExpectedConditions are used to wait for a specific criteria to be satisfied. This method pauses whenever one of the below incidents happen −

  • The timeout duration specified has elapsed.

  • The criteria defined yields neither false nor null.

We can have a custom ExpectedCondition by creating an object of expected criteria and by taking the help of apply method.

Let us take an example of the below page. Let us click on the Team link.

On clicking on Team, a corresponding paragraph appears on right. Let us verify if the paragraph has appeared and also verify the text India appears within that paragraph.


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.concurrent.TimeUnit;
public class CustomExpCondition{
   public static void main(String[] args)
      throws InterruptedException{
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      // identify element
      WebElement l=driver.findElement(By.linkText("Team"));;
      //object of WebDriverWait class with wait time
      WebDriverWait w = new WebDriverWait(driver,7);
      //custom expected condition with until method
      w.until(new ExpectedCondition <Boolean> (){
         public Boolean apply(WebDriver driver) {
            //identify paragraph
            WebElement e= driver.findElement(By.tagName("p"));
            if (e!= null){
               //to check if paragraph is displayed and has text India
               if (e.isDisplayed() && e.getText().contains("India")) {
                  System.out.println("Element found");
                  return true;
               else {
                  System.out.println("Element not found");
                  return false;
            return false;


Updated on 01-Feb-2021 11:46:26