Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/support/wait.py
3990 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
import time
19
from collections.abc import Callable, Iterable
20
from typing import Generic, Literal, TypeVar
21
22
from selenium.common.exceptions import NoSuchElementException, TimeoutException
23
from selenium.webdriver.remote.webdriver import WebDriver
24
from selenium.webdriver.remote.webelement import WebElement
25
26
POLL_FREQUENCY: float = 0.5 # How long to sleep in between calls to the method
27
IGNORED_EXCEPTIONS: tuple[type[Exception]] = (NoSuchElementException,) # default to be ignored.
28
29
D = TypeVar("D", bound=WebDriver | WebElement)
30
T = TypeVar("T")
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`.
91
92
Raises:
93
TimeoutException: If 'method' does not return a truthy value within
94
the WebDriverWait object's timeout.
95
96
Example:
97
>>> from selenium.webdriver.common.by import By
98
>>> from selenium.webdriver.support.ui import WebDriverWait
99
>>> from selenium.webdriver.support import expected_conditions as EC
100
>>>
101
>>> # Wait until an element is visible on the page
102
>>> wait = WebDriverWait(driver, 10)
103
>>> element = wait.until(EC.visibility_of_element_located((By.ID, "exampleId")))
104
>>> print(element.text)
105
"""
106
screen = None
107
stacktrace = None
108
109
end_time = time.monotonic() + self._timeout
110
while True:
111
try:
112
value = method(self._driver)
113
if value:
114
return value
115
except self._ignored_exceptions as exc:
116
screen = getattr(exc, "screen", None)
117
stacktrace = getattr(exc, "stacktrace", None)
118
if time.monotonic() > end_time:
119
break
120
time.sleep(self._poll)
121
raise TimeoutException(message, screen, stacktrace)
122
123
def until_not(self, method: Callable[[D], T], message: str = "") -> T | Literal[True]:
124
"""Wait until the method returns a value that is False.
125
126
Calls the method provided with the driver as an argument until the
127
return value evaluates to ``False``.
128
129
Args:
130
method: A callable object that takes a WebDriver instance as an
131
argument.
132
message: Optional message for TimeoutException.
133
134
Returns:
135
The result of the last call to `method`.
136
137
Raises:
138
TimeoutException: If 'method' does not return False within the
139
WebDriverWait object's timeout.
140
141
Example:
142
>>> from selenium.webdriver.common.by import By
143
>>> from selenium.webdriver.support.ui import WebDriverWait
144
>>> from selenium.webdriver.support import expected_conditions as EC
145
>>>
146
>>> # Wait until an element is no longer visible on the page
147
>>> wait = WebDriverWait(driver, 10)
148
>>> is_disappeared = wait.until_not(EC.visibility_of_element_located((By.ID, "exampleId")))
149
"""
150
end_time = time.monotonic() + self._timeout
151
while True:
152
try:
153
value = method(self._driver)
154
if not value:
155
return value
156
except self._ignored_exceptions:
157
return True
158
if time.monotonic() > end_time:
159
break
160
time.sleep(self._poll)
161
raise TimeoutException(message)
162
163