Skip to content

Commit b8de36f

Browse files
[py] implement relative locator for find_element (#9902)
Co-authored-by: David Burns <[email protected]>
1 parent 18e1d42 commit b8de36f

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

py/selenium/webdriver/remote/webdriver.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,9 @@ def find_element(self, by=By.ID, value=None) -> WebElement:
12201220
12211221
:rtype: WebElement
12221222
"""
1223+
if isinstance(by, RelativeBy):
1224+
return self.find_elements(by=by, value=value)[0]
1225+
12231226
if by == By.ID:
12241227
by = By.CSS_SELECTOR
12251228
value = '[id="%s"]' % value

py/test/selenium/webdriver/support/relative_by_tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
from selenium.webdriver.support.relative_locator import with_tag_name, locate_with
2020

2121

22+
def test_should_be_able_to_find_first_one(driver, pages):
23+
pages.load("relative_locators.html")
24+
lowest = driver.find_element(By.ID, "below")
25+
26+
el = driver.find_element(with_tag_name("p").above(lowest))
27+
28+
assert el.get_attribute('id') == "mid"
29+
30+
2231
def test_should_be_able_to_find_elements_above_another(driver, pages):
2332
pages.load("relative_locators.html")
2433
lowest = driver.find_element(By.ID, "below")

0 commit comments

Comments
 (0)