Path: blob/trunk/py/selenium/webdriver/support/wait.py
1864 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 typing import Callable, Generic, Literal, Optional, TypeVar, Union1920from selenium.common.exceptions import NoSuchElementException, TimeoutException21from selenium.types import WaitExcTypes22from 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=Union[WebDriver, WebElement])29T = TypeVar("T")303132class WebDriverWait(Generic[D]):33def __init__(34self,35driver: D,36timeout: float,37poll_frequency: float = POLL_FREQUENCY,38ignored_exceptions: Optional[WaitExcTypes] = None,39):40"""Constructor, takes a WebDriver instance and timeout in seconds.4142Attributes:43-----------44driver45- Instance of WebDriver (Ie, Firefox, Chrome or Remote) or46a WebElement4748timeout49- Number of seconds before timing out5051poll_frequency52- Sleep interval between calls53- By default, it is 0.5 second.5455ignored_exceptions56- Iterable structure of exception classes ignored during calls.57- By default, it contains NoSuchElementException only.5859Example:60--------61>>> from selenium.webdriver.common.by import By62>>> from selenium.webdriver.support.wait import WebDriverWait63>>> from selenium.common.exceptions import ElementNotVisibleException64>>>65>>> # Wait until the element is no longer visible66>>> is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException))67... .until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())68"""69self._driver = driver70self._timeout = float(timeout)71self._poll = poll_frequency72# avoid the divide by zero73if self._poll == 0:74self._poll = POLL_FREQUENCY75exceptions: list = list(IGNORED_EXCEPTIONS)76if ignored_exceptions:77try:78exceptions.extend(iter(ignored_exceptions))79except TypeError: # ignored_exceptions is not iterable80exceptions.append(ignored_exceptions)81self._ignored_exceptions = tuple(exceptions)8283def __repr__(self) -> str:84return f'<{type(self).__module__}.{type(self).__name__} (session="{self._driver.session_id}")>'8586def until(self, method: Callable[[D], Union[Literal[False], T]], message: str = "") -> T:87"""Wait until the method returns a value that is not False.8889Calls the method provided with the driver as an argument until the90return value does not evaluate to ``False``.9192Parameters:93-----------94method: callable(WebDriver)95- A callable object that takes a WebDriver instance as an argument.9697message: str98- Optional message for :exc:`TimeoutException`99100Return:101-------102object: T103- The result of the last call to `method`104105Raises:106-------107TimeoutException108- If 'method' does not return a truthy value within the WebDriverWait109object's timeout110111Example:112--------113>>> from selenium.webdriver.common.by import By114>>> from selenium.webdriver.support.ui import WebDriverWait115>>> from selenium.webdriver.support import expected_conditions as EC116117# Wait until an element is visible on the page118>>> wait = WebDriverWait(driver, 10)119>>> element = wait.until(EC.visibility_of_element_located((By.ID, "exampleId")))120>>> print(element.text)121"""122screen = None123stacktrace = None124125end_time = time.monotonic() + self._timeout126while True:127try:128value = method(self._driver)129if value:130return value131except self._ignored_exceptions as exc:132screen = getattr(exc, "screen", None)133stacktrace = getattr(exc, "stacktrace", None)134if time.monotonic() > end_time:135break136time.sleep(self._poll)137raise TimeoutException(message, screen, stacktrace)138139def until_not(self, method: Callable[[D], T], message: str = "") -> Union[T, Literal[True]]:140"""Wait until the method returns a value that is not False.141142Calls the method provided with the driver as an argument until the143return value does not evaluate to ``False``.144145Parameters:146-----------147method: callable(WebDriver)148- A callable object that takes a WebDriver instance as an argument.149150message: str151- Optional message for :exc:`TimeoutException`152153Return:154-------155object: T156- The result of the last call to `method`157158Raises:159-------160TimeoutException161- If 'method' does not return False within the WebDriverWait162object's timeout163164Example:165--------166>>> from selenium.webdriver.common.by import By167>>> from selenium.webdriver.support.ui import WebDriverWait168>>> from selenium.webdriver.support import expected_conditions as EC169170# Wait until an element is visible on the page171>>> wait = WebDriverWait(driver, 10)172>>> is_disappeared = wait.until_not(EC.visibility_of_element_located((By.ID, "exampleId")))173"""174end_time = time.monotonic() + self._timeout175while True:176try:177value = method(self._driver)178if not value:179return value180except self._ignored_exceptions:181return True182if time.monotonic() > end_time:183break184time.sleep(self._poll)185raise TimeoutException(message)186187188