Skip to content

Commit

Permalink
[py] implement relative locator for find_element (#9902)
Browse files Browse the repository at this point in the history
Co-authored-by: David Burns <david.burns@theautomatedtester.co.uk>
  • Loading branch information
titusfortner and AutomatedTester committed Oct 12, 2021
1 parent 18e1d42 commit b8de36f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions py/selenium/webdriver/remote/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,9 @@ def find_element(self, by=By.ID, value=None) -> WebElement:
:rtype: WebElement
"""
if isinstance(by, RelativeBy):
return self.find_elements(by=by, value=value)[0]

if by == By.ID:
by = By.CSS_SELECTOR
value = '[id="%s"]' % value
Expand Down
9 changes: 9 additions & 0 deletions py/test/selenium/webdriver/support/relative_by_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
from selenium.webdriver.support.relative_locator import with_tag_name, locate_with


def test_should_be_able_to_find_first_one(driver, pages):
pages.load("relative_locators.html")
lowest = driver.find_element(By.ID, "below")

el = driver.find_element(with_tag_name("p").above(lowest))

assert el.get_attribute('id') == "mid"


def test_should_be_able_to_find_elements_above_another(driver, pages):
pages.load("relative_locators.html")
lowest = driver.find_element(By.ID, "below")
Expand Down

0 comments on commit b8de36f

Please sign in to comment.