Understanding ExpectedConditions in Selenium (with Types and Examples)
Related Product On This Page What is Expectedconditions in Selenium WebDriver?April 03, 2026 · 9 min read · Tool Comparison
When automating web application with to load properly is crucial.Expected Conditions in Seleniumprovide a reliable way to wait for specific states, like element profile, clickability, or alert, ensuring test are stable and less flaky. Types of Expected Conditions in Selenium This article will explainExpectedconditionscommonly used in with examples. allows for waiting for specific conditions until a defined task is complete. An example is automating the task to check if all ingredient represent on a web page, matching a special, are seeable. Syntax: Now, let us explore the various eccentric of Selenium Expectedconditions and their uses. Read more: Here are some of the eccentric of Expected Conditions in Selenium: ExpectedCondition & lt; WebElement & gt; This condition has a web element locator as a parameter. An explicit wait can be applied to the condition that tries to happen the web element in question. If the status finds the element, it returns the element as a result. If not, the wait command tries the condition again after a short delay. ExpectedCondition & lt; Boolean & gt; This stipulation has a twine parameter, and the delay command applies the condition to the parameter. If the result is true, then the value true is returned. If the result is false, the wait command tries the condition again after a short delay. While the applies to the expect status, the condition code may render various elision. Have a look at a few of the expected conditions: 1. static ExpectedCondition & lt; WebElement & gt; elementToBeClickable (By locater) This condition is used to instruct a command to wait until the element is clickable by the locater. 2. static ExpectedCondition & lt; Boolean & gt; elementToBeSelected (By locator) This condition instructs a bid to wait until the locator selects the ingredient. 3. electrostatic ExpectedCondition & lt; WebElement & gt; presenceOfElementLocated (By locater) This condition instructs a command to wait until the component becomes visible or present. 4. static ExpectedCondition & lt; Boolean & gt; titleContains (String title) This condition is used to instruct a bidding to check if the title of the web element or the webpage check the specific String or the group of characters. 5. unchanging ExpectedCondition & lt; Boolean & gt; titleIs (String rubric) This precondition is used to instruct a dictation to ascertain whether the title is the String or the group of characters. 6.static ExpectedCondition & lt; Boolean & gt; urlToBe (String url) Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script. This condition is used to instruct a command to ensure if the URL of the webpage matches the expected URL. 7. static ExpectedCondition & lt; WebElement & gt; visibilityOfElementLocated (By locator) This status apprize a command to look until the element go seeable. To learn about this in outstanding detail, concern to thisofficial sourceon ExpectedConditions Also Read: The next example applies Expectedconditions to the Facebook login page. The codification has use Facebook sign-up credentials and located them. It has also created a generic function to create it available for all constituent to provide Explicit wait. The code apply Expected Conditions for the visibility of Element. The driver will look for 20 seconds to find if the element is visible. Then, using the, it will enter the credentials to log in. Note: To use Expectedconditions in a Selenium script, import the following packages: Now, interpret how to make a Custom Expectedcondition in Selenium. Read More: These method frequently help synchronise exam with web page behaviour: A Custom ExpectedCondition is a class that lie of a constructor with the parameters of the expected condition. It implements the ExpectedCondition interface and overrides the apply method. The following example will demo how to make a custom expectedcondition: Executing the above codification, it will verify whether the abode page URL and the result page URL lucifer. Selenium will likewise check whether both URLs are display and wait until the page is loaded. Explicit waits leveragingExpectedConditionsto hesitate test executing until a specified stipulation is met. Some additional utilitarian conditions include: Read More: Selenium & # 8217; s ExpectedConditions grade, when hold with explicit waits, has multiple benefits that make the machine-controlled tests more authentic and effective. Here are Good Practices for UsingExpectedConditionin Selenium: Read More: Running Selenium trial on existent devices ensures that your web application behaves as expected in real-world weather. can & # 8217; t fully replicate device-specific quirks, performance issues, or browser behaviors. Real devices reveal layout shifts, render bugs, or touch responsiveness problems that often go unnoticed in virtual environments. Key Benefits: Use to run Selenium tests on 3,500+ existent devices and browser without maintaining infrastructure. It enables fast, scalable testing straightaway integrated with your CI pipeline—so you can get issue early and freeing with confidence. Including Selenium & # 8217; sExpectedConditionsin explicit waits enhances test automation. These weather offer fine-grained control over element states so that interactions only happen when elements are available. Such behavior lower screen flakiness and increases stableness by eliminating timing-related failure. Furthermore, explicit postponement optimize the execution of tests by go as presently as conditions are fulfilled, eliminate unnecessary wait multiplication. Generally, utilizingExpectedConditionsmakes test script more stable, efficient, and maintainable, particularly when address with dynamic web pages. On This Page # Ask-and-Contributeabout this theme with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.Related Product
Understanding ExpectedConditions in Selenium (with Types and Examples)
Overview
What is Expectedconditions in Selenium WebDriver?
static ExpectedCondition & lt; WebElement & gt; visibilityOfElementLocated (By locator)
Types of Expectedconditions
Example of Selenium Expectedconditions
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; importee org.openqa.selenium.WebDriver; signification org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public course ExpectedCondtionsExample {public static void independent (String [] args) throws InterruptedException {System.setProperty (`` webdriver.chrome.driver '', `` Path of the driver ''); WebDriver driver = new ChromeDriver (); driver.manage () .window () .maximize (); driver.manage () .deleteAllCookies (); driver.manage () .timeouts () .pageLoadTimeout (40, TimeUnit.SECONDS); driver.manage () .timeouts () .implicitlyWait (30, TimeUnit.SECONDS); driver.get (`` https: //www.facebook.com/ ''); WebElement fname= driver.findElement (By.name (`` fname '')); WebElement lname= driver.findElement (By.name (`` lname '')); sendKeys (driver, fname, 10, `` Your_Name ''); sendKeys (driver, lname, 20, `` Your_Lastname ''); WebElement forgotAccount= driver.findElement (By.linkText (`` Forgotten account? ``)); clickOn (driver, forgotAccount, 10); driver.manage () .timeouts () .implicitlyWait (30, TimeUnit.SECONDS);} //sendkeys method public static void sendKeys (WebDriver driver1, WebElement element, int timeout, String value) {new WebDriverWait (driver1, timeout) .until (ExpectedConditions.visibilityOf (ingredient)); element.sendKeys (value);} //clickable method declared explicitly public unchanging void clickOn (WebDriver driver1, WebElement factor, int timeout) {new WebDriverWait (driver1, timeout) .until (ExpectedConditions.elementToBeClickable (element)); // Expectedcondition for the element to be clickable element.click ();}}import org.openqa.selenium.support.ui.ExpectedConditions; signification org.openqa.selenium.support.ui.WebDriverWait;
Commonly Used Methods With ExpectedConditions
Custom Expectedcondition in Selenium
public class CustomConditionExample {WebDriver driver; WebDriverWait wait; By searchFieldXpath = By.id (`` twotabsearchtextbox ''); By searchButtonXpath = By.className (`` nav-search-submit-text nav-sprite ''); By resultLinkLocator = By.xpath (`` //span [@ class='celwidget slot=SEARCH_RESULTS template=SEARCH_RESULTS widgetId=search-results index=0 '] //div [@ class= ' a-section aok-relative s-image-fixed-height '] ''); String homeUrl = `` https: //www.amazon.com/ ''; String homeTitle = `` Amazon.com: Online Shopping for Electronics, Apparel, Computers, ... ''; String resultsTitle = `` Search | kindle paperwhite e-reader ''; String resultsUrl = `` https: //www.amazon.com/s? k=kindle+paperwhite+e-reader ''; @ Before public void setUp ()} driver = new FirefoxDriver (); wait = new WebDriverWait (driver, 10);} @ After public void tearDown () {driver.quit ();} @ Test public void test1 () {driver.get (siteUrl); if (! wait.until (new PageLoaded (homeTitle, homeUrl))) cast new RuntimeException (`` home page is not displayed ''); WebElement searchField = wait.until (elementToBeClickable (searchFieldXpath)); searchField.click (); searchField.sendKeys (keyword); WebElement searchButton = wait.until (elementToBeClickable (searchButtonXpath)); searchButton.click (); if (! wait.until (new PageLoaded (resultsTitle, resultsUrl))) throw new RuntimeException (`` results page is not displayed '');}} public class PageLoaded implements ExpectedCondition {String expectedTitle; String expectedUrl; public PageLoaded (String expectedTitle, String expectedUrl) {this.expectedTitle = expectedTitle; this.expectedUrl = expectedUrl;} @ Override public Boolean apply (WebDriver driver) {Boolean isTitleCorrect = driver.getTitle () .contains (expectedTitle); Boolean isUrlCorrect = driver.getCurrentUrl () .contains (expectedUrl); return isTitleCorrect & amp; & amp; isUrlCorrect;}}ExpectedCondition that can be apply in Explicit Waits
Advantages of Selenium Expectedconditions
Better Practices for utilise ExpectedCondition in Selenium
Why Run Selenium Tests on Real Devices?
Conclusion
Related Guides
Automate This With SUSA
Test Your App Autonomously