Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/common/exceptions.py
4021 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 Any
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: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = 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: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = 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 not have a shadow root attached."""
95
96
97
class StaleElementReferenceException(WebDriverException):
98
"""Thrown when a reference to an element is now "stale".
99
100
Stale means the element no longer appears on the DOM of the page.
101
102
103
Possible causes of StaleElementReferenceException include, but not limited to:
104
* You are no longer on the same page, or the page may have refreshed since the element
105
was located.
106
* The element may have been removed and re-added to the screen, since it was located.
107
Such as an element being relocated.
108
This can happen typically with a javascript framework when values are updated and the
109
node is rebuilt.
110
* Element may have been inside an iframe or another context which was refreshed.
111
"""
112
113
def __init__(
114
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
115
) -> None:
116
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#staleelementreferenceexception"
117
118
super().__init__(with_support, screen, stacktrace)
119
120
121
class InvalidElementStateException(WebDriverException):
122
"""Thrown when a command could not be completed because the element is in an invalid state.
123
124
This can be caused by attempting to clear an element that isn't both editable and resettable.
125
"""
126
127
128
class UnexpectedAlertPresentException(WebDriverException):
129
"""Thrown when an unexpected alert has appeared.
130
131
Usually raised when an unexpected modal is blocking the webdriver
132
from executing commands.
133
"""
134
135
def __init__(
136
self,
137
msg: Any | None = None,
138
screen: str | None = None,
139
stacktrace: Sequence[str] | None = None,
140
alert_text: str | None = None,
141
) -> None:
142
super().__init__(msg, screen, stacktrace)
143
self.alert_text = alert_text
144
145
def __str__(self) -> str:
146
return f"Alert Text: {self.alert_text}\n{super().__str__()}"
147
148
149
class NoAlertPresentException(WebDriverException):
150
"""Thrown when switching to no presented alert.
151
152
This can be caused by calling an operation on the Alert() class when
153
an alert is not yet on the screen.
154
"""
155
156
157
class ElementNotVisibleException(InvalidElementStateException):
158
"""Thrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with.
159
160
Most commonly encountered when trying to click or read text of an element that is hidden from view.
161
"""
162
163
def __init__(
164
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
165
) -> None:
166
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementnotvisibleexception"
167
168
super().__init__(with_support, screen, stacktrace)
169
170
171
class ElementNotInteractableException(InvalidElementStateException):
172
"""Thrown when element interactions will hit another element due to paint order."""
173
174
def __init__(
175
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
176
) -> None:
177
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementnotinteractableexception"
178
179
super().__init__(with_support, screen, stacktrace)
180
181
182
class ElementNotSelectableException(InvalidElementStateException):
183
"""Thrown when trying to select an unselectable element.
184
185
For example, selecting a 'script' element.
186
"""
187
188
189
class InvalidCookieDomainException(WebDriverException):
190
"""Thrown when attempting to add a cookie under a different domain."""
191
192
193
class UnableToSetCookieException(WebDriverException):
194
"""Thrown when a driver fails to set a cookie."""
195
196
197
class TimeoutException(WebDriverException):
198
"""Thrown when a command does not complete in enough time."""
199
200
201
class MoveTargetOutOfBoundsException(WebDriverException):
202
"""Thrown when the target provided to the `ActionsChains` move() method is invalid, i.e. out of document."""
203
204
205
class UnexpectedTagNameException(WebDriverException):
206
"""Thrown when a support class did not get an expected web element."""
207
208
209
class InvalidSelectorException(WebDriverException):
210
"""Thrown when the selector used to find an element does not return a WebElement.
211
212
Currently this only happens when the XPath expression is syntactically invalid or does not select WebElements.
213
"""
214
215
def __init__(
216
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
217
) -> None:
218
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#invalidselectorexception"
219
220
super().__init__(with_support, screen, stacktrace)
221
222
223
class ImeNotAvailableException(WebDriverException):
224
"""Thrown when IME support is not available.
225
226
This exception is thrown for every IME-related method call if IME
227
support is not available on the machine.
228
"""
229
230
231
class ImeActivationFailedException(WebDriverException):
232
"""Thrown when activating an IME engine has failed."""
233
234
235
class InvalidArgumentException(WebDriverException):
236
"""The arguments passed to a command are either invalid or malformed."""
237
238
239
class JavascriptException(WebDriverException):
240
"""An error occurred while executing JavaScript supplied by the user."""
241
242
243
class NoSuchCookieException(WebDriverException):
244
"""Thrown when no cookie matching the given path name was found."""
245
246
247
class ScreenshotException(WebDriverException):
248
"""A screen capture was made impossible."""
249
250
251
class ElementClickInterceptedException(WebDriverException):
252
"""Thrown when element click fails because another element obscures it."""
253
254
def __init__(
255
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
256
) -> None:
257
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#elementclickinterceptedexception"
258
259
super().__init__(with_support, screen, stacktrace)
260
261
262
class InsecureCertificateException(WebDriverException):
263
"""Thrown when the user agent hits a certificate warning (expired or invalid TLS certificate)."""
264
265
266
class InvalidCoordinatesException(WebDriverException):
267
"""The coordinates provided to an interaction's operation are invalid."""
268
269
270
class InvalidSessionIdException(WebDriverException):
271
"""Thrown when the given session id is not in the list of active sessions."""
272
273
def __init__(
274
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
275
) -> None:
276
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#invalidsessionidexception"
277
278
super().__init__(with_support, screen, stacktrace)
279
280
281
class SessionNotCreatedException(WebDriverException):
282
"""A new session could not be created."""
283
284
def __init__(
285
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
286
) -> None:
287
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}#sessionnotcreatedexception"
288
289
super().__init__(with_support, screen, stacktrace)
290
291
292
class UnknownMethodException(WebDriverException):
293
"""The requested command matched a known URL but did not match any methods for that URL."""
294
295
296
class NoSuchDriverException(WebDriverException):
297
"""Raised when driver is not specified and cannot be located."""
298
299
def __init__(
300
self, msg: Any | None = None, screen: str | None = None, stacktrace: Sequence[str] | None = None
301
) -> None:
302
with_support = f"{msg}; {SUPPORT_MSG} {ERROR_URL}/driver_location"
303
304
super().__init__(with_support, screen, stacktrace)
305
306
307
class DetachedShadowRootException(WebDriverException):
308
"""Raised when referenced shadow root is no longer attached to the DOM."""
309
310