selenium.webdriver.support.ui#

class Select(webelement)[source]#

Bases: object

Constructor. A check is made that the given element is a SELECT tag.

Parameters:

webelement (WebElement) – SELECT element to wrap

Example

from selenium.webdriver.support.ui import Select Select(driver.find_element(By.TAG_NAME, “select”)).select_by_index(2)

Raises:

UnexpectedTagNameException – If the element is not a SELECT tag

Parameters:

webelement (WebElement)

property options: list[WebElement]#

Returns a list of all options belonging to this select tag.

property all_selected_options: list[WebElement]#

Return a list of all selected options belonging to this select tag.

property first_selected_option: WebElement#

Return the first selected option or the currently selected option.

select_by_value(value)[source]#

Select all options that have a value matching the argument.

Example

When given “foo” this would select an option like:

<option value=”foo”>Bar</option>

Parameters:

value (str) – The value to match against

Raises:

NoSuchElementException – If there is no option with specified value in SELECT

Return type:

None

select_by_index(index)[source]#

Select the option at the given index by examining the “index” attribute.

Parameters:

index (int) – The option at this index will be selected

Raises:

NoSuchElementException – If there is no option with specified index in SELECT

Return type:

None

select_by_visible_text(text)[source]#

Select all options that display text matching the argument.

Example

When given “Bar” this would select an option like:

<option value=”foo”>Bar</option>

Parameters:

text (str) – The visible text to match against

Raises:

NoSuchElementException – If there is no option with specified text in SELECT

Return type:

None

deselect_all()[source]#

Clear all selected entries.

This is only valid when the SELECT supports multiple selections. throws NotImplementedError If the SELECT does not support multiple selections

Return type:

None

deselect_by_value(value)[source]#

Deselect all options that have a value matching the argument.

Example

When given “foo” this would deselect an option like:

<option value=”foo”>Bar</option>

Parameters:

value (str) – The value to match against

Raises:

NoSuchElementException – If there is no option with specified value in SELECT

Return type:

None

deselect_by_index(index)[source]#

Deselect the option at the given index by examining the “index” attribute.

Parameters:

index (int) – The option at this index will be deselected

Raises:

NoSuchElementException – If there is no option with specified index in SELECT

Return type:

None

deselect_by_visible_text(text)[source]#

Deselect all options that display text matching the argument.

Example

when given “Bar” this would deselect an option like:

<option value=”foo”>Bar</option>

Parameters:

text (str) – The visible text to match against

Return type:

None

class WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)[source]#

Bases: Generic[D]

Constructor, takes a WebDriver instance and timeout in seconds.

Parameters:
  • driver (D) – Instance of WebDriver (Ie, Firefox, Chrome or Remote) or a WebElement.

  • timeout (float) – Number of seconds before timing out.

  • poll_frequency (float) – Sleep interval between calls. By default, it is 0.5 second.

  • ignored_exceptions (Iterable[type[Exception]] | None) – Iterable structure of exception classes ignored during calls. By default, it contains NoSuchElementException only.

Example

>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.wait import WebDriverWait
>>> from selenium.common.exceptions import ElementNotVisibleException
>>>
>>> # Wait until the element is no longer visible
>>> is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException))
...     .until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())
until(method, message='')[source]#

Wait until the method returns a value that is not False.

Calls the method provided with the driver as an argument until the return value does not evaluate to False.

Parameters:
  • method (Callable[[D], Literal[False] | ~selenium.webdriver.support.wait.T]) – A callable object that takes a WebDriver instance as an argument.

  • message (str) – Optional message for TimeoutException.

Returns:

The result of the last call to method.

Raises:

TimeoutException – If ‘method’ does not return a truthy value within the WebDriverWait object’s timeout.

Return type:

T

Example

>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.wait import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>>
>>> # Wait until an element is visible on the page
>>> wait = WebDriverWait(driver, 10)
>>> element = wait.until(EC.visibility_of_element_located((By.ID, "exampleId")))
>>> print(element.text)
until_not(method, message='')[source]#

Wait until the method returns a value that is False.

Calls the method provided with the driver as an argument until the return value evaluates to False.

Parameters:
  • method (Callable[[D], T]) – A callable object that takes a WebDriver instance as an argument.

  • message (str) – Optional message for TimeoutException.

Returns:

The result of the last call to method.

Raises:

TimeoutException – If ‘method’ does not return False within the WebDriverWait object’s timeout.

Return type:

T | Literal[True]

Example

>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.wait import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>>
>>> # Wait until an element is no longer visible on the page
>>> wait = WebDriverWait(driver, 10)
>>> is_disappeared = wait.until_not(EC.visibility_of_element_located((By.ID, "exampleId")))