Path: blob/trunk/py/selenium/webdriver/support/wait.py
3990 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import time18from collections.abc import Callable, Iterable19from typing import Generic, Literal, TypeVar2021from selenium.common.exceptions import NoSuchElementException, TimeoutException22from selenium.webdriver.remote.webdriver import WebDriver23from selenium.webdriver.remote.webelement import WebElement2425POLL_FREQUENCY: float = 0.5 # How long to sleep in between calls to the method26IGNORED_EXCEPTIONS: tuple[type[Exception]] = (NoSuchElementException,) # default to be ignored.2728D = TypeVar("D", bound=WebDriver | WebElement)29T = TypeVar("T")303132class WebDriverWait(Generic[D]):33def __init__(34self,35driver: D,36timeout: float,37poll_frequency: float = POLL_FREQUENCY,38ignored_exceptions: Iterable[type[Exception]] | None = None,39):40"""Constructor, takes a WebDriver instance and timeout in seconds.4142Args:43driver: Instance of WebDriver (Ie, Firefox, Chrome or Remote) or44a WebElement.45timeout: Number of seconds before timing out.46poll_frequency: Sleep interval between calls. By default, it is470.5 second.48ignored_exceptions: Iterable structure of exception classes ignored49during calls. By default, it contains NoSuchElementException only.5051Example:52>>> from selenium.webdriver.common.by import By53>>> from selenium.webdriver.support.wait import WebDriverWait54>>> from selenium.common.exceptions import ElementNotVisibleException55>>>56>>> # Wait until the element is no longer visible57>>> is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException))58... .until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())59"""60self._driver = driver61self._timeout = float(timeout)62self._poll = poll_frequency63# avoid the divide by zero64if self._poll == 0:65self._poll = POLL_FREQUENCY66exceptions: list = list(IGNORED_EXCEPTIONS)67if ignored_exceptions:68try:69exceptions.extend(iter(ignored_exceptions))70except TypeError: # ignored_exceptions is not iterable71exceptions.append(ignored_exceptions)72self._ignored_exceptions = tuple(exceptions)7374def __repr__(self) -> str:75return f'<{type(self).__module__}.{type(self).__name__} (session="{self._driver.session_id}")>'7677def until(self, method: Callable[[D], Literal[False] | T], message: str = "") -> T:78"""Wait until the method returns a value that is not False.7980Calls the method provided with the driver as an argument until the81return value does not evaluate to ``False``.8283Args:84method: A callable object that takes a WebDriver instance as an85argument.86message: Optional message for TimeoutException.8788Returns:89The result of the last call to `method`.9091Raises:92TimeoutException: If 'method' does not return a truthy value within93the WebDriverWait object's timeout.9495Example:96>>> from selenium.webdriver.common.by import By97>>> from selenium.webdriver.support.ui import WebDriverWait98>>> from selenium.webdriver.support import expected_conditions as EC99>>>100>>> # Wait until an element is visible on the page101>>> wait = WebDriverWait(driver, 10)102>>> element = wait.until(EC.visibility_of_element_located((By.ID, "exampleId")))103>>> print(element.text)104"""105screen = None106stacktrace = None107108end_time = time.monotonic() + self._timeout109while True:110try:111value = method(self._driver)112if value:113return value114except self._ignored_exceptions as exc:115screen = getattr(exc, "screen", None)116stacktrace = getattr(exc, "stacktrace", None)117if time.monotonic() > end_time:118break119time.sleep(self._poll)120raise TimeoutException(message, screen, stacktrace)121122def until_not(self, method: Callable[[D], T], message: str = "") -> T | Literal[True]:123"""Wait until the method returns a value that is False.124125Calls the method provided with the driver as an argument until the126return value evaluates to ``False``.127128Args:129method: A callable object that takes a WebDriver instance as an130argument.131message: Optional message for TimeoutException.132133Returns:134The result of the last call to `method`.135136Raises:137TimeoutException: If 'method' does not return False within the138WebDriverWait object's timeout.139140Example:141>>> from selenium.webdriver.common.by import By142>>> from selenium.webdriver.support.ui import WebDriverWait143>>> from selenium.webdriver.support import expected_conditions as EC144>>>145>>> # Wait until an element is no longer visible on the page146>>> wait = WebDriverWait(driver, 10)147>>> is_disappeared = wait.until_not(EC.visibility_of_element_located((By.ID, "exampleId")))148"""149end_time = time.monotonic() + self._timeout150while True:151try:152value = method(self._driver)153if not value:154return value155except self._ignored_exceptions:156return True157if time.monotonic() > end_time:158break159time.sleep(self._poll)160raise TimeoutException(message)161162163