selenium.webdriver.support.relative_locator

Functions

locate_with(by, using)

Start searching for relative objects your search criteria with By.

with_tag_name(tag_name)

Start searching for relative objects using a tag name.

Classes

RelativeBy([root, filters])

Gives the opportunity to find elements based on their relative location on the page from a root elelemt.

selenium.webdriver.support.relative_locator.with_tag_name(tag_name: str) RelativeBy[source]

Start searching for relative objects using a tag name.

Note: This method may be removed in future versions, please use locate_with instead. :Args:

  • tag_name: the DOM tag of element to start searching.

Returns:
  • RelativeBy - use this object to create filters within a

    find_elements call.

selenium.webdriver.support.relative_locator.locate_with(by: By, using: str) RelativeBy[source]

Start searching for relative objects your search criteria with By.

Args:
  • by: The value from By passed in.

  • using: search term to find the element with.

Returns:
  • RelativeBy - use this object to create filters within a

    find_elements call.

class selenium.webdriver.support.relative_locator.RelativeBy(root: Dict[By | str, str] | None = None, filters: List | None = None)[source]

Gives the opportunity to find elements based on their relative location on the page from a root elelemt. It is recommended that you use the helper function to create it.

Example:

lowest = driver.find_element(By.ID, “below”)

elements = driver.find_elements(locate_with(By.CSS_SELECTOR, “p”).above(lowest))

ids = [el.get_attribute(‘id’) for el in elements] assert “above” in ids assert “mid” in ids

Creates a new RelativeBy object. It is preferred if you use the locate_with method as this signature could change.

Args:

root - A dict with By enum as the key and the search query as the value filters - A list of the filters that will be searched. If none are passed

in please use the fluent API on the object to create the filters

above(element_or_locator: WebElement | Dict | None = None) RelativeBy[source]

Add a filter to look for elements above.

Args:
  • element_or_locator: Element to look above

below(element_or_locator: WebElement | Dict | None = None) RelativeBy[source]

Add a filter to look for elements below.

Args:
  • element_or_locator: Element to look below

to_left_of(element_or_locator: WebElement | Dict | None = None) RelativeBy[source]

Add a filter to look for elements to the left of.

Args:
  • element_or_locator: Element to look to the left of

to_right_of(element_or_locator: WebElement | Dict | None = None) RelativeBy[source]

Add a filter to look for elements right of.

Args:
  • element_or_locator: Element to look right of

near(element_or_locator_distance: WebElement | Dict | int | None = None) RelativeBy[source]

Add a filter to look for elements near.

Args:
  • element_or_locator_distance: Element to look near by the element or within a distance

to_dict() Dict[source]

Create a dict that will be passed to the driver to start searching for the element.