Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/common/exceptions.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
"""Exceptions that may happen in all the webdriver code."""
18
19
from collections.abc import Sequence
20
from typing import Optional
21
22
SUPPORT_MSG = "For documentation on this error, please visit:"
23
ERROR_URL = "https://www.selenium.dev/documentation/webdriver/troubleshooting/errors"
24
25
26
class WebDriverException(Exception):
27
"""Base webdriver exception."""
28
29
def __init__(
30
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
31
) -> None:
32
super().__init__()
33
self.msg = msg
34
self.screen = screen
35
self.stacktrace = stacktrace
36
37
def __str__(self) -> str:
38
exception_msg = f"Message: {self.msg}\n"
39
if self.screen:
40
exception_msg += "Screenshot: available via screen\n"
41
if self.stacktrace:
42
stacktrace = "\n".join(self.stacktrace)
43
exception_msg += f"Stacktrace:\n{stacktrace}"
44
return exception_msg
45
46
47
class InvalidSwitchToTargetException(WebDriverException):
48
"""Thrown when frame or window target to be switched doesn't exist."""
49
50
51
class NoSuchFrameException(InvalidSwitchToTargetException):
52
"""Thrown when frame target to be switched doesn't exist."""
53
54
55
class NoSuchWindowException(InvalidSwitchToTargetException):
56
"""Thrown when window target to be switched doesn't exist.
57
58
To find the current set of active window handles, you can get a list
59
of the active window handles in the following way::
60
61
print driver.window_handles
62
"""
63
64
65
class NoSuchElementException(WebDriverException):
66
"""Thrown when element could not be found.
67
68
If you encounter this exception, you may want to check the following:
69
* Check your selector used in your find_by...
70
* Element may not yet be on the screen at the time of the find operation,
71
(webpage is still loading) see selenium.webdriver.support.wait.WebDriverWait()
72
for how to write a wait wrapper to wait for an element to appear.
73
"""
74
75
def __init__(
76
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
77
) -> None:
78
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#nosuchelementexception"
79
80
super().__init__(with_support, screen, stacktrace)
81
82
83
class NoSuchAttributeException(WebDriverException):
84
"""Thrown when the attribute of element could not be found.
85
86
You may want to check if the attribute exists in the particular
87
browser you are testing against. Some browsers may have different
88
property names for the same property. (IE8's .innerText vs. Firefox
89
.textContent)
90
"""
91
92
93
class NoSuchShadowRootException(WebDriverException):
94
"""Thrown when trying to access the shadow root of an element when it does
95
not have a shadow root attached."""
96
97
98
class StaleElementReferenceException(WebDriverException):
99
"""Thrown when a reference to an element is now "stale".
100
101
Stale means the element no longer appears on the DOM of the page.
102
103
104
Possible causes of StaleElementReferenceException include, but not limited to:
105
* You are no longer on the same page, or the page may have refreshed since the element
106
was located.
107
* The element may have been removed and re-added to the screen, since it was located.
108
Such as an element being relocated.
109
This can happen typically with a javascript framework when values are updated and the
110
node is rebuilt.
111
* Element may have been inside an iframe or another context which was refreshed.
112
"""
113
114
def __init__(
115
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
116
) -> None:
117
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#staleelementreferenceexception"
118
119
super().__init__(with_support, screen, stacktrace)
120
121
122
class InvalidElementStateException(WebDriverException):
123
"""Thrown when a command could not be completed because the element is in
124
an invalid state.
125
126
This can be caused by attempting to clear an element that isn't both
127
editable and resettable.
128
"""
129
130
131
class UnexpectedAlertPresentException(WebDriverException):
132
"""Thrown when an unexpected alert has appeared.
133
134
Usually raised when an unexpected modal is blocking the webdriver
135
from executing commands.
136
"""
137
138
def __init__(
139
self,
140
msg: Optional[str] = None,
141
screen: Optional[str] = None,
142
stacktrace: Optional[Sequence[str]] = None,
143
alert_text: Optional[str] = None,
144
) -> None:
145
super().__init__(msg, screen, stacktrace)
146
self.alert_text = alert_text
147
148
def __str__(self) -> str:
149
return f"Alert Text: {self.alert_text}\n{super().__str__()}"
150
151
152
class NoAlertPresentException(WebDriverException):
153
"""Thrown when switching to no presented alert.
154
155
This can be caused by calling an operation on the Alert() class when
156
an alert is not yet on the screen.
157
"""
158
159
160
class ElementNotVisibleException(InvalidElementStateException):
161
"""Thrown when an element is present on the DOM, but it is not visible, and
162
so is not able to be interacted with.
163
164
Most commonly encountered when trying to click or read text of an
165
element that is hidden from view.
166
"""
167
168
def __init__(
169
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
170
) -> None:
171
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementnotvisibleexception"
172
173
super().__init__(with_support, screen, stacktrace)
174
175
176
class ElementNotInteractableException(InvalidElementStateException):
177
"""Thrown when an element is present in the DOM but interactions with that
178
element will hit another element due to paint order."""
179
180
def __init__(
181
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
182
) -> None:
183
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementnotinteractableexception"
184
185
super().__init__(with_support, screen, stacktrace)
186
187
188
class ElementNotSelectableException(InvalidElementStateException):
189
"""Thrown when trying to select an unselectable element.
190
191
For example, selecting a 'script' element.
192
"""
193
194
195
class InvalidCookieDomainException(WebDriverException):
196
"""Thrown when attempting to add a cookie under a different domain than the
197
current URL."""
198
199
200
class UnableToSetCookieException(WebDriverException):
201
"""Thrown when a driver fails to set a cookie."""
202
203
204
class TimeoutException(WebDriverException):
205
"""Thrown when a command does not complete in enough time."""
206
207
208
class MoveTargetOutOfBoundsException(WebDriverException):
209
"""Thrown when the target provided to the `ActionsChains` move() method is
210
invalid, i.e. out of document."""
211
212
213
class UnexpectedTagNameException(WebDriverException):
214
"""Thrown when a support class did not get an expected web element."""
215
216
217
class InvalidSelectorException(WebDriverException):
218
"""Thrown when the selector which is used to find an element does not
219
return a WebElement.
220
221
Currently this only happens when the selector is an xpath expression
222
and it is either syntactically invalid (i.e. it is not a xpath
223
expression) or the expression does not select WebElements (e.g.
224
"count(//input)").
225
"""
226
227
def __init__(
228
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
229
) -> None:
230
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#invalidselectorexception"
231
232
super().__init__(with_support, screen, stacktrace)
233
234
235
class ImeNotAvailableException(WebDriverException):
236
"""Thrown when IME support is not available.
237
238
This exception is thrown for every IME-related method call if IME
239
support is not available on the machine.
240
"""
241
242
243
class ImeActivationFailedException(WebDriverException):
244
"""Thrown when activating an IME engine has failed."""
245
246
247
class InvalidArgumentException(WebDriverException):
248
"""The arguments passed to a command are either invalid or malformed."""
249
250
251
class JavascriptException(WebDriverException):
252
"""An error occurred while executing JavaScript supplied by the user."""
253
254
255
class NoSuchCookieException(WebDriverException):
256
"""No cookie matching the given path name was found amongst the associated
257
cookies of the current browsing context's active document."""
258
259
260
class ScreenshotException(WebDriverException):
261
"""A screen capture was made impossible."""
262
263
264
class ElementClickInterceptedException(WebDriverException):
265
"""The Element Click command could not be completed because the element
266
receiving the events is obscuring the element that was requested to be
267
clicked."""
268
269
def __init__(
270
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
271
) -> None:
272
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementclickinterceptedexception"
273
274
super().__init__(with_support, screen, stacktrace)
275
276
277
class InsecureCertificateException(WebDriverException):
278
"""Navigation caused the user agent to hit a certificate warning, which is
279
usually the result of an expired or invalid TLS certificate."""
280
281
282
class InvalidCoordinatesException(WebDriverException):
283
"""The coordinates provided to an interaction's operation are invalid."""
284
285
286
class InvalidSessionIdException(WebDriverException):
287
"""Occurs if the given session id is not in the list of active sessions,
288
meaning the session either does not exist or that it's not active."""
289
290
def __init__(
291
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
292
) -> None:
293
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#invalidsessionidexception"
294
295
super().__init__(with_support, screen, stacktrace)
296
297
298
class SessionNotCreatedException(WebDriverException):
299
"""A new session could not be created."""
300
301
def __init__(
302
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
303
) -> None:
304
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#sessionnotcreatedexception"
305
306
super().__init__(with_support, screen, stacktrace)
307
308
309
class UnknownMethodException(WebDriverException):
310
"""The requested command matched a known URL but did not match any methods
311
for that URL."""
312
313
314
class NoSuchDriverException(WebDriverException):
315
"""Raised when driver is not specified and cannot be located."""
316
317
def __init__(
318
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
319
) -> None:
320
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}/driver_location"
321
322
super().__init__(with_support, screen, stacktrace)
323
324
325
class DetachedShadowRootException(WebDriverException):
326
"""Raised when referenced shadow root is no longer attached to the DOM."""
327
328