selenium.webdriver.support.expected_conditions

Functions

alert_is_present()

Check that an alert is present and switch to it.

all_of(*expected_conditions)

An expectation that all of multiple expected conditions is true.

any_of(*expected_conditions)

An expectation that any of multiple expected conditions is true.

element_attribute_to_include(locator, attribute_)

Check if the given attribute is included in the specified element.

element_located_selection_state_to_be(...)

Check that an element's selection state matches the expected state.

element_located_to_be_selected(locator)

An expectation for the element to be located is selected.

element_selection_state_to_be(element, ...)

An expectation for checking if the given element is selected.

element_to_be_clickable(mark)

Check that an element is visible and enabled so it can be clicked.

element_to_be_selected(element)

An expectation for checking the selection is selected.

frame_to_be_available_and_switch_to_it(locator)

Check that the given frame is available and switch to it.

invisibility_of_element(element)

Check that an element is either invisible or not present on the DOM.

invisibility_of_element_located(locator)

Check that an element is either invisible or not present on the DOM.

new_window_is_opened(current_handles)

Check that a new window has been opened (window handles count increased).

none_of(*expected_conditions)

An expectation that none of 1 or multiple expected conditions is true.

number_of_windows_to_be(num_windows)

An expectation for the number of windows to be a certain value.

presence_of_all_elements_located(locator)

Check that all elements matching the locator are present on the DOM.

presence_of_element_located(locator)

Check that an element is present on the DOM (not necessarily visible).

staleness_of(element)

Wait until an element is no longer attached to the DOM.

text_to_be_present_in_element(locator, text_)

Check that the given text is present in the specified element.

text_to_be_present_in_element_attribute(...)

Check that the given text is present in the element's attribute.

text_to_be_present_in_element_value(locator, ...)

Check that the given text is present in the element's value.

title_contains(title)

Check that the title contains a case-sensitive substring.

title_is(title)

An expectation for checking the title of a page.

url_changes(url)

Check that the current url differs from a given string.

url_contains(url)

Check that the current url contains a case-sensitive substring.

url_matches(pattern)

An expectation for checking the current url.

url_to_be(url)

An expectation for checking the current url.

visibility_of(element)

Check that an element is visible (present in DOM and width/height greater than zero).

visibility_of_all_elements_located(locator)

Check that all elements are visible (present in DOM and width/height greater than zero).

visibility_of_any_elements_located(locator)

Check that at least one element is visible on the web page (present in DOM and width/height greater than zero).

visibility_of_element_located(locator)

Check that an element is visible (present in DOM and width/height greater than zero).

selenium.webdriver.support.expected_conditions.title_is(title: str) Callable[[WebDriver], bool][source]

An expectation for checking the title of a page.

Args:

title: The expected title, which must be an exact match.

Returns:

True if the title matches, False otherwise.

selenium.webdriver.support.expected_conditions.title_contains(title: str) Callable[[WebDriver], bool][source]

Check that the title contains a case-sensitive substring.

Args:

title: The fragment of title expected.

Returns:

True when the title matches, False otherwise.

selenium.webdriver.support.expected_conditions.presence_of_element_located(locator: tuple[str, str]) Callable[[WebDriver | WebElement], WebElement][source]

Check that an element is present on the DOM (not necessarily visible).

Args:

locator: Used to find the element.

Returns:

The WebElement once it is located.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, “q”)))

selenium.webdriver.support.expected_conditions.url_contains(url: str) Callable[[WebDriver], bool][source]

Check that the current url contains a case-sensitive substring.

Args:

url: The fragment of url expected.

Returns:

True when the url matches, False otherwise.

selenium.webdriver.support.expected_conditions.url_matches(pattern: str) Callable[[WebDriver], bool][source]

An expectation for checking the current url.

Args:

pattern: The pattern to match with the current url.

Returns:

True when the pattern matches, False otherwise.

Note:

More powerful than url_contains, as it allows for regular expressions.

selenium.webdriver.support.expected_conditions.url_to_be(url: str) Callable[[WebDriver], bool][source]

An expectation for checking the current url.

Args:

url: The expected url, which must be an exact match.

Returns:

True when the url matches, False otherwise.

selenium.webdriver.support.expected_conditions.url_changes(url: str) Callable[[WebDriver], bool][source]

Check that the current url differs from a given string.

Args:

url: The expected url, which must not be an exact match.

Returns:

True when the url does not match, False otherwise.

selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator: tuple[str, str]) Callable[[WebDriver | WebElement], Literal[False] | WebElement][source]

Check that an element is visible (present in DOM and width/height greater than zero).

Args:

locator: Used to find the element.

Returns:

The WebElement once it is located and visible.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.NAME, “q”)))

selenium.webdriver.support.expected_conditions.visibility_of(element: WebElement) Callable[[Any], Literal[False] | WebElement][source]

Check that an element is visible (present in DOM and width/height greater than zero).

Args:

element: The WebElement to check.

Returns:

The WebElement once it is visible.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element(By.NAME, “q”)))

selenium.webdriver.support.expected_conditions.presence_of_all_elements_located(locator: tuple[str, str]) Callable[[WebDriver | WebElement], list[WebElement]][source]

Check that all elements matching the locator are present on the DOM.

Args:

locator: Used to find the element.

Returns:

The list of WebElements once they are located.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC elements = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.CLASS_NAME, “foo”)))

selenium.webdriver.support.expected_conditions.visibility_of_any_elements_located(locator: tuple[str, str]) Callable[[WebDriver | WebElement], list[WebElement]][source]

Check that at least one element is visible on the web page (present in DOM and width/height greater than zero).

Args:

locator: Used to find the element.

Returns:

The list of WebElements once they are located and visible.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC elements = WebDriverWait(driver, 10).until(EC.visibility_of_any_elements_located((By.CLASS_NAME, “foo”)))

selenium.webdriver.support.expected_conditions.visibility_of_all_elements_located(locator: tuple[str, str]) Callable[[WebDriver | WebElement], list[WebElement] | Literal[False]][source]

Check that all elements are visible (present in DOM and width/height greater than zero).

Args:

locator: Used to find the elements.

Returns:

The list of WebElements once they are located and visible.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC elements = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CLASS_NAME, “foo”)))

selenium.webdriver.support.expected_conditions.text_to_be_present_in_element(locator: tuple[str, str], text_: str) Callable[[WebDriver | WebElement], bool][source]

Check that the given text is present in the specified element.

Args:

locator: Used to find the element. text_: The text to be present in the element.

Returns:

True when the text is present, False otherwise.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_text_in_element = WebDriverWait(driver, 10).until(

EC.text_to_be_present_in_element((By.CLASS_NAME, “foo”), “bar”)

)

selenium.webdriver.support.expected_conditions.text_to_be_present_in_element_value(locator: tuple[str, str], text_: str) Callable[[WebDriver | WebElement], bool][source]

Check that the given text is present in the element’s value.

Args:

locator: Used to find the element. text_: The text to be present in the element’s value.

Returns:

True when the text is present, False otherwise.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_text_in_element_value = WebDriverWait(driver, 10).until(

EC.text_to_be_present_in_element_value((By.CLASS_NAME, “foo”), “bar”)

)

selenium.webdriver.support.expected_conditions.text_to_be_present_in_element_attribute(locator: tuple[str, str], attribute_: str, text_: str) Callable[[WebDriver | WebElement], bool][source]

Check that the given text is present in the element’s attribute.

Args:

locator: Used to find the element. attribute_: The attribute to check the text in. text_: The text to be present in the element’s attribute.

Returns:

True when the text is present, False otherwise.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_text_in_element_attribute = WebDriverWait(driver, 10).until(

EC.text_to_be_present_in_element_attribute((By.CLASS_NAME, “foo”), “bar”, “baz”)

)

selenium.webdriver.support.expected_conditions.frame_to_be_available_and_switch_to_it(locator: tuple[str, str] | str | WebElement) Callable[[WebDriver], bool][source]

Check that the given frame is available and switch to it.

Args:

locator: Used to find the frame.

Returns:

True when the frame is available, False otherwise.

Example:

from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(“frame_name”))

selenium.webdriver.support.expected_conditions.invisibility_of_element_located(locator: WebElement | tuple[str, str]) Callable[[WebDriver | WebElement], WebElement | bool][source]

Check that an element is either invisible or not present on the DOM.

Args:

locator: Used to find the element.

Returns:

True when the element is invisible or not present, False otherwise.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_invisible = WebDriverWait(driver, 10).until(EC.invisibility_of_element_located((By.CLASS_NAME, “foo”)))

Note:

In the case of NoSuchElement, returns true because the element is not present in DOM. The try block checks if the element is present but is invisible. In the case of StaleElementReference, returns true because stale element reference implies that element is no longer visible.

selenium.webdriver.support.expected_conditions.invisibility_of_element(element: WebElement | tuple[str, str]) Callable[[WebDriver | WebElement], WebElement | bool][source]

Check that an element is either invisible or not present on the DOM.

Args:

element: Used to find the element.

Returns:

True when the element is invisible or not present, False otherwise.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_invisible_or_not_present = WebDriverWait(driver, 10).until(

EC.invisibility_of_element(driver.find_element(By.CLASS_NAME, “foo”))

)

selenium.webdriver.support.expected_conditions.element_to_be_clickable(mark: WebElement | tuple[str, str]) Callable[[WebDriver | WebElement], Literal[False] | WebElement][source]

Check that an element is visible and enabled so it can be clicked.

Args:

mark: Used to find the element.

Returns:

The WebElement once it is located and clickable.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, “foo”)))

selenium.webdriver.support.expected_conditions.staleness_of(element: WebElement) Callable[[Any], bool][source]

Wait until an element is no longer attached to the DOM.

Args:

element: The element to wait for.

Returns:

False if the element is still attached to the DOM, true otherwise.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_stale = WebDriverWait(driver, 10).until(EC.staleness_of(driver.find_element(By.CLASS_NAME, “foo”)))

selenium.webdriver.support.expected_conditions.element_to_be_selected(element: WebElement) Callable[[Any], bool][source]

An expectation for checking the selection is selected.

Args:

element: The WebElement to check.

Returns:

True if the element is selected, False otherwise.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_selected = WebDriverWait(driver, 10).until(EC.element_to_be_selected(driver.find_element(

By.CLASS_NAME, “foo”))

)

selenium.webdriver.support.expected_conditions.element_located_to_be_selected(locator: tuple[str, str]) Callable[[WebDriver | WebElement], bool][source]

An expectation for the element to be located is selected.

Args:

locator: Used to find the element.

Returns:

True if the element is selected, False otherwise.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_selected = WebDriverWait(driver, 10).until(EC.element_located_to_be_selected((By.CLASS_NAME, “foo”)))

selenium.webdriver.support.expected_conditions.element_selection_state_to_be(element: WebElement, is_selected: bool) Callable[[Any], bool][source]

An expectation for checking if the given element is selected.

Args:

element: The WebElement to check. is_selected: The expected selection state.

Returns:

True if the element’s selection state is the same as is_selected.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_selected = WebDriverWait(driver, 10).until(

EC.element_selection_state_to_be(driver.find_element(By.CLASS_NAME, “foo”), True)

)

selenium.webdriver.support.expected_conditions.element_located_selection_state_to_be(locator: tuple[str, str], is_selected: bool) Callable[[WebDriver | WebElement], bool][source]

Check that an element’s selection state matches the expected state.

Args:

locator: Used to find the element. is_selected: The expected selection state.

Returns:

True if the element’s selection state is the same as is_selected.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_selected = WebDriverWait(driver, 10).until(EC.element_located_selection_state_to_be(

(By.CLASS_NAME, “foo”), True)

)

selenium.webdriver.support.expected_conditions.number_of_windows_to_be(num_windows: int) Callable[[WebDriver], bool][source]

An expectation for the number of windows to be a certain value.

Args:

num_windows: The expected number of windows.

Returns:

True when the number of windows matches, False otherwise.

Example:

from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_number_of_windows = WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))

selenium.webdriver.support.expected_conditions.new_window_is_opened(current_handles: set[str]) Callable[[WebDriver], bool][source]

Check that a new window has been opened (window handles count increased).

Args:

current_handles: The current window handles.

Returns:

True when a new window is opened, False otherwise.

Example:

from selenium.webdriver.support.ui import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_new_window_opened = WebDriverWait(driver, 10).until(EC.new_window_is_opened(driver.window_handles))

selenium.webdriver.support.expected_conditions.alert_is_present() Callable[[WebDriver], Alert | bool][source]

Check that an alert is present and switch to it.

Returns:

The Alert once it is located.

Example:

from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC alert = WebDriverWait(driver, 10).until(EC.alert_is_present())

Note:

If the alert is present it switches the given driver to it.

selenium.webdriver.support.expected_conditions.element_attribute_to_include(locator: tuple[str, str], attribute_: str) Callable[[WebDriver | WebElement], bool][source]

Check if the given attribute is included in the specified element.

Args:

locator: Used to find the element. attribute_: The attribute to check.

Returns:

True when the attribute is included, False otherwise.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC is_attribute_in_element = WebDriverWait(driver, 10).until(

EC.element_attribute_to_include((By.CLASS_NAME, “foo”), “bar”)

)

selenium.webdriver.support.expected_conditions.any_of(*expected_conditions: Callable[[D], T]) Callable[[D], Literal[False] | T][source]

An expectation that any of multiple expected conditions is true.

Equivalent to a logical ‘OR’. Returns results of the first matching condition, or False if none do.

Args:

expected_conditions: The list of expected conditions to check.

Returns:

The result of the first matching condition, or False if none do.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until(

EC.any_of(EC.presence_of_element_located((By.NAME, “q”), EC.visibility_of_element_located((By.NAME, “q”)))

)

selenium.webdriver.support.expected_conditions.all_of(*expected_conditions: Callable[[D], T | Literal[False]]) Callable[[D], list[T] | Literal[False]][source]

An expectation that all of multiple expected conditions is true.

Equivalent to a logical ‘AND’. When any ExpectedCondition is not met, returns False. When all ExpectedConditions are met, returns a List with each ExpectedCondition’s return value.

Args:

expected_conditions: The list of expected conditions to check.

Returns:

The results of all the matching conditions, or False if any do not.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC elements = WebDriverWait(driver, 10).until(

EC.all_of(EC.presence_of_element_located((By.NAME, “q”), EC.visibility_of_element_located((By.NAME, “q”)))

)

selenium.webdriver.support.expected_conditions.none_of(*expected_conditions: Callable[[D], Any]) Callable[[D], bool][source]

An expectation that none of 1 or multiple expected conditions is true.

Equivalent to a logical ‘NOT-OR’.

Args:

expected_conditions: The list of expected conditions to check.

Returns:

True if none of the conditions are true, False otherwise.

Example:

from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC element = WebDriverWait(driver, 10).until(

EC.none_of(EC.presence_of_element_located((By.NAME, “q”), EC.visibility_of_element_located((By.NAME, “q”)))

)