| 31 | |
| 32 | |
| 33 | class WebDriverWait(Generic[D]): |
| 34 | def __init__( |
| 35 | self, |
| 36 | driver: D, |
| 37 | timeout: float, |
| 38 | poll_frequency: float = POLL_FREQUENCY, |
| 39 | ignored_exceptions: Iterable[type[Exception]] | None = None, |
| 40 | ): |
| 41 | """Constructor, takes a WebDriver instance and timeout in seconds. |
| 42 | |
| 43 | Args: |
| 44 | driver: Instance of WebDriver (Ie, Firefox, Chrome or Remote) or |
| 45 | a WebElement. |
| 46 | timeout: Number of seconds before timing out. |
| 47 | poll_frequency: Sleep interval between calls. By default, it is |
| 48 | 0.5 second. |
| 49 | ignored_exceptions: Iterable structure of exception classes ignored |
| 50 | during calls. By default, it contains NoSuchElementException only. |
| 51 | |
| 52 | Example: |
| 53 | >>> from selenium.webdriver.common.by import By |
| 54 | >>> from selenium.webdriver.support.wait import WebDriverWait |
| 55 | >>> from selenium.common.exceptions import ElementNotVisibleException |
| 56 | >>> |
| 57 | >>> # Wait until the element is no longer visible |
| 58 | >>> is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)) |
| 59 | ... .until_not(lambda x: x.find_element(By.ID, "someId").is_displayed()) |
| 60 | """ |
| 61 | self._driver = driver |
| 62 | self._timeout = float(timeout) |
| 63 | self._poll = poll_frequency |
| 64 | # avoid the divide by zero |
| 65 | if self._poll == 0: |
| 66 | self._poll = POLL_FREQUENCY |
| 67 | exceptions: list = list(IGNORED_EXCEPTIONS) |
| 68 | if ignored_exceptions: |
| 69 | try: |
| 70 | exceptions.extend(iter(ignored_exceptions)) |
| 71 | except TypeError: # ignored_exceptions is not iterable |
| 72 | exceptions.append(ignored_exceptions) |
| 73 | self._ignored_exceptions = tuple(exceptions) |
| 74 | |
| 75 | def __repr__(self) -> str: |
| 76 | return f'<{type(self).__module__}.{type(self).__name__} (session="{self._driver.session_id}")>' |
| 77 | |
| 78 | def until(self, method: Callable[[D], Literal[False] | T], message: str = "") -> T: |
| 79 | """Wait until the method returns a value that is not False. |
| 80 | |
| 81 | Calls the method provided with the driver as an argument until the |
| 82 | return value does not evaluate to ``False``. |
| 83 | |
| 84 | Args: |
| 85 | method: A callable object that takes a WebDriver instance as an |
| 86 | argument. |
| 87 | message: Optional message for TimeoutException. |
| 88 | |
| 89 | Returns: |
| 90 | The result of the last call to `method`. |
no outgoing calls