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 element.

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

Start searching for relative objects using a tag name.

Parameters:

tag_namestr

The DOM tag of element to start searching.

Returns:

RelativeBy

Use this object to create filters within a find_elements call.

Raises:

WebDriverException

If tag_name is None.

Notes:

  • This method is deprecated and may be removed in future versions.

  • Please use locate_with instead.

selenium.webdriver.support.relative_locator.locate_with(by: Literal['id', 'xpath', 'link text', 'partial link text', 'name', 'tag name', 'class name', 'css selector'], using: str) RelativeBy[source]

Start searching for relative objects your search criteria with By.

Parameters:

byByType

The method to find the element.

usingstr

The value from By passed in.

Returns:

RelativeBy

Use this object to create filters within a find_elements call.

Example:

>>> lowest = driver.find_element(By.ID, "below")
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").above(lowest))
class selenium.webdriver.support.relative_locator.RelativeBy(root: Dict[Literal['id', 'xpath', 'link text', 'partial link text', 'name', 'tag name', 'class name', 'css selector'], 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 element. 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.

Attributes:

rootDict[By, str]
  • A dict with By enum as the key and the search query as the value

filtersList
  • 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

LocatorType

alias of Dict[Literal[‘id’, ‘xpath’, ‘link text’, ‘partial link text’, ‘name’, ‘tag name’, ‘class name’, ‘css selector’], str]

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

Add a filter to look for elements above.

Parameters:

element_or_locatorUnion[WebElement, Dict, None]

Element to look above

Returns:

RelativeBy

Raises:

WebDriverException

If element_or_locator is None.

Example:

>>> lowest = driver.find_element(By.ID, "below")
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").above(lowest))
below(element_or_locator: WebElement | LocatorType) RelativeBy[source]
below(element_or_locator: None = None) NoReturn

Add a filter to look for elements below.

Parameters:

element_or_locatorUnion[WebElement, Dict, None]

Element to look below

Returns:

RelativeBy

Raises:

WebDriverException

If element_or_locator is None.

Example:

>>> highest = driver.find_element(By.ID, "high")
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").below(highest))
to_left_of(element_or_locator: WebElement | LocatorType) RelativeBy[source]
to_left_of(element_or_locator: None = None) NoReturn

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

Parameters:

element_or_locatorUnion[WebElement, Dict, None]

Element to look to the left of

Returns:

RelativeBy

Raises:

WebDriverException

If element_or_locator is None.

Example:

>>> right = driver.find_element(By.ID, "right")
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").to_left_of(right))
to_right_of(element_or_locator: WebElement | LocatorType) RelativeBy[source]
to_right_of(element_or_locator: None = None) NoReturn

Add a filter to look for elements right of.

Parameters:

element_or_locatorUnion[WebElement, Dict, None]

Element to look right of

Returns:

RelativeBy

Raises:

WebDriverException

If element_or_locator is None.

Example:

>>> left = driver.find_element(By.ID, "left")
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").to_right_of(left))
straight_above(element_or_locator: WebElement | LocatorType) RelativeBy[source]
straight_above(element_or_locator: None = None) NoReturn

Add a filter to look for elements above.

Args:
  • element_or_locator: Element to look above

straight_below(element_or_locator: WebElement | LocatorType) RelativeBy[source]
straight_below(element_or_locator: None = None) NoReturn

Add a filter to look for elements below.

Args:
  • element_or_locator: Element to look below

straight_left_of(element_or_locator: WebElement | LocatorType) RelativeBy[source]
straight_left_of(element_or_locator: None = None) NoReturn

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

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

straight_right_of(element_or_locator: WebElement | LocatorType) RelativeBy[source]
straight_right_of(element_or_locator: None = None) NoReturn

Add a filter to look for elements right of.

Args:
  • element_or_locator: Element to look right of

near(element_or_locator: WebElement | LocatorType, distance: int = 50) RelativeBy[source]
near(element_or_locator: None = None, distance: int = 50) NoReturn

Add a filter to look for elements near.

Parameters:

element_or_locatorUnion[WebElement, Dict, None]

Element to look near by the element or within a distance

distanceint

Distance in pixel

Returns:

RelativeBy

Raises:

WebDriverException
  • If element_or_locator is None

  • If distance is less than or equal to 0.

Example:

>>> near = driver.find_element(By.ID, "near")
>>> elements = driver.find_elements(locate_with(By.CSS_SELECTOR, "p").near(near, 50))
to_dict() Dict[source]

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