diff --git a/py/selenium/webdriver/remote/webdriver.py b/py/selenium/webdriver/remote/webdriver.py index c3567d7ac4af6..160ad5e43b427 100644 --- a/py/selenium/webdriver/remote/webdriver.py +++ b/py/selenium/webdriver/remote/webdriver.py @@ -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 diff --git a/py/test/selenium/webdriver/support/relative_by_tests.py b/py/test/selenium/webdriver/support/relative_by_tests.py index 6f3af4d90c6e8..0070393e129d6 100644 --- a/py/test/selenium/webdriver/support/relative_by_tests.py +++ b/py/test/selenium/webdriver/support/relative_by_tests.py @@ -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")