Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/support/wait.py
1864 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 typing import Callable, Generic, Literal, Optional, TypeVar, Union
20
21
from selenium.common.exceptions import NoSuchElementException, TimeoutException
22
from selenium.types import WaitExcTypes
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=Union[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: Optional[WaitExcTypes] = None,
40
):
41
"""Constructor, takes a WebDriver instance and timeout in seconds.
42
43
Attributes:
44
-----------
45
driver
46
- Instance of WebDriver (Ie, Firefox, Chrome or Remote) or
47
a WebElement
48
49
timeout
50
- Number of seconds before timing out
51
52
poll_frequency
53
- Sleep interval between calls
54
- By default, it is 0.5 second.
55
56
ignored_exceptions
57
- Iterable structure of exception classes ignored during calls.
58
- By default, it contains NoSuchElementException only.
59
60
Example:
61
--------
62
>>> from selenium.webdriver.common.by import By
63
>>> from selenium.webdriver.support.wait import WebDriverWait
64
>>> from selenium.common.exceptions import ElementNotVisibleException
65
>>>
66
>>> # Wait until the element is no longer visible
67
>>> is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException))
68
... .until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())
69
"""
70
self._driver = driver
71
self._timeout = float(timeout)
72
self._poll = poll_frequency
73
# avoid the divide by zero
74
if self._poll == 0:
75
self._poll = POLL_FREQUENCY
76
exceptions: list = list(IGNORED_EXCEPTIONS)
77
if ignored_exceptions:
78
try:
79
exceptions.extend(iter(ignored_exceptions))
80
except TypeError: # ignored_exceptions is not iterable
81
exceptions.append(ignored_exceptions)
82
self._ignored_exceptions = tuple(exceptions)
83
84
def __repr__(self) -> str:
85
return f'<{type(self).__module__}.{type(self).__name__} (session="{self._driver.session_id}")>'
86
87
def until(self, method: Callable[[D], Union[Literal[False], T]], message: str = "") -> T:
88
"""Wait until the method returns a value that is not False.
89
90
Calls the method provided with the driver as an argument until the
91
return value does not evaluate to ``False``.
92
93
Parameters:
94
-----------
95
method: callable(WebDriver)
96
- A callable object that takes a WebDriver instance as an argument.
97
98
message: str
99
- Optional message for :exc:`TimeoutException`
100
101
Return:
102
-------
103
object: T
104
- The result of the last call to `method`
105
106
Raises:
107
-------
108
TimeoutException
109
- If 'method' does not return a truthy value within the WebDriverWait
110
object's timeout
111
112
Example:
113
--------
114
>>> from selenium.webdriver.common.by import By
115
>>> from selenium.webdriver.support.ui import WebDriverWait
116
>>> from selenium.webdriver.support import expected_conditions as EC
117
118
# Wait until an element is visible on the page
119
>>> wait = WebDriverWait(driver, 10)
120
>>> element = wait.until(EC.visibility_of_element_located((By.ID, "exampleId")))
121
>>> print(element.text)
122
"""
123
screen = None
124
stacktrace = None
125
126
end_time = time.monotonic() + self._timeout
127
while True:
128
try:
129
value = method(self._driver)
130
if value:
131
return value
132
except self._ignored_exceptions as exc:
133
screen = getattr(exc, "screen", None)
134
stacktrace = getattr(exc, "stacktrace", None)
135
if time.monotonic() > end_time:
136
break
137
time.sleep(self._poll)
138
raise TimeoutException(message, screen, stacktrace)
139
140
def until_not(self, method: Callable[[D], T], message: str = "") -> Union[T, Literal[True]]:
141
"""Wait until the method returns a value that is not False.
142
143
Calls the method provided with the driver as an argument until the
144
return value does not evaluate to ``False``.
145
146
Parameters:
147
-----------
148
method: callable(WebDriver)
149
- A callable object that takes a WebDriver instance as an argument.
150
151
message: str
152
- Optional message for :exc:`TimeoutException`
153
154
Return:
155
-------
156
object: T
157
- The result of the last call to `method`
158
159
Raises:
160
-------
161
TimeoutException
162
- If 'method' does not return False within the WebDriverWait
163
object's timeout
164
165
Example:
166
--------
167
>>> from selenium.webdriver.common.by import By
168
>>> from selenium.webdriver.support.ui import WebDriverWait
169
>>> from selenium.webdriver.support import expected_conditions as EC
170
171
# Wait until an element is visible on the page
172
>>> wait = WebDriverWait(driver, 10)
173
>>> is_disappeared = wait.until_not(EC.visibility_of_element_located((By.ID, "exampleId")))
174
"""
175
end_time = time.monotonic() + self._timeout
176
while True:
177
try:
178
value = method(self._driver)
179
if not value:
180
return value
181
except self._ignored_exceptions:
182
return True
183
if time.monotonic() > end_time:
184
break
185
time.sleep(self._poll)
186
raise TimeoutException(message)
187
188