The ExpectedConditions class

The Selenium WebDriver's ExpectedConditions class provides users with common methods to check for specific conditions of elements on a page. Those conditions include such things as:

  • Titles
  • URLs
  • Presence of elements
  • Visibility of elements
  • Text on elements
  • Frames to switch to
  • Invisibility of elements
  • Element-clickable states
  • Staleness of elements
  • Refreshing elements
  • Element selection states
  • Alerts
  • Number of windows
  • Finding elements
  • Attributes of elements
  • Number of elements
  • Nested elements
  • JavaScript values

Using the ExpectedConditions class's methods is simple. You would just call them as follows:

ExpectedConditions.visibilityOf(WebElement element)

Alternatively, you can use:

ExpectedConditions.visibilityOfElementLocated(By by)

These two methods do the same thing, except one takes a static locator as a parameter, and the second one takes a dynamically generated locator. But using these methods alone is not enough. It is imperative to wait "up to" a designated time period before throwing an exception that the element is not found. This can be done by passing the result of these methods to the WebDriverWait class's methods.