Created
May 12, 2025 16:08
-
-
Save derrickturk/3f02ee361fd649bd4598e5327a06f79a to your computer and use it in GitHub Desktop.
Selenium: wait until an element can be found by relative locator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium.common import NoSuchElementException | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.remote.webdriver import WebDriver | |
from selenium.webdriver.remote.webelement import WebElement | |
from selenium.webdriver.support.relative_locator import RelativeBy, locate_with | |
class WaitRelative: | |
def __init__(self, by: RelativeBy): | |
self._by = by | |
def __call__(self, driver: WebDriver) -> WebElement | Literal[False]: | |
try: | |
return driver.find_element(self._by) | |
except NoSuchElementException: | |
return False | |
def wait_relative(driver: WebDriver, by: RelativeBy, timeout: float = 30 | |
) -> WebElement: | |
return WebDriverWait(driver, timeout).until(WaitRelative(by)) | |
e = wait_relative( | |
locate_with(By.CSS_SELECTOR, 'div[data-name="Shared"]').below(other)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment