Skip to content

Commit eaa7ecc

Browse files
committed
[py]: tidy exceptions.py code
1 parent fe9444d commit eaa7ecc

File tree

3 files changed

+50
-72
lines changed

3 files changed

+50
-72
lines changed

py/selenium/common/__init__.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,38 @@
4949
from .exceptions import UnknownMethodException
5050
from .exceptions import WebDriverException
5151

52-
__all__ = ["WebDriverException",
53-
"InvalidSwitchToTargetException",
54-
"NoSuchFrameException",
55-
"NoSuchWindowException",
56-
"NoSuchElementException",
57-
"NoSuchAttributeException",
58-
"NoSuchShadowRootException",
59-
"StaleElementReferenceException",
60-
"InvalidElementStateException",
61-
"UnexpectedAlertPresentException",
62-
"NoAlertPresentException",
63-
"ElementNotVisibleException",
64-
"ElementNotInteractableException",
65-
"ElementNotSelectableException",
66-
"InvalidCookieDomainException",
67-
"UnableToSetCookieException",
68-
"RemoteDriverServerException",
69-
"TimeoutException",
70-
"MoveTargetOutOfBoundsException",
71-
"UnexpectedTagNameException",
72-
"InvalidSelectorException",
73-
"ImeNotAvailableException",
74-
"ImeActivationFailedException",
75-
"InvalidArgumentException",
76-
"JavascriptException",
77-
"NoSuchCookieException",
78-
"ScreenshotException",
79-
"ElementClickInterceptedException",
80-
"InsecureCertificateException",
81-
"InvalidCoordinatesException",
82-
"InvalidSessionIdException",
83-
"SessionNotCreatedException",
84-
"UnknownMethodException"]
52+
__all__ = [
53+
"WebDriverException",
54+
"InvalidSwitchToTargetException",
55+
"NoSuchFrameException",
56+
"NoSuchWindowException",
57+
"NoSuchElementException",
58+
"NoSuchAttributeException",
59+
"NoSuchShadowRootException",
60+
"StaleElementReferenceException",
61+
"InvalidElementStateException",
62+
"UnexpectedAlertPresentException",
63+
"NoAlertPresentException",
64+
"ElementNotVisibleException",
65+
"ElementNotInteractableException",
66+
"ElementNotSelectableException",
67+
"InvalidCookieDomainException",
68+
"UnableToSetCookieException",
69+
"RemoteDriverServerException",
70+
"TimeoutException",
71+
"MoveTargetOutOfBoundsException",
72+
"UnexpectedTagNameException",
73+
"InvalidSelectorException",
74+
"ImeNotAvailableException",
75+
"ImeActivationFailedException",
76+
"InvalidArgumentException",
77+
"JavascriptException",
78+
"NoSuchCookieException",
79+
"ScreenshotException",
80+
"ElementClickInterceptedException",
81+
"InsecureCertificateException",
82+
"InvalidCoordinatesException",
83+
"InvalidSessionIdException",
84+
"SessionNotCreatedException",
85+
"UnknownMethodException",
86+
]

py/selenium/common/exceptions.py

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,33 @@ class WebDriverException(Exception):
2828
Base webdriver exception.
2929
"""
3030

31-
def __init__(self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None) -> None:
31+
def __init__(
32+
self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None
33+
) -> None:
3234
self.msg = msg
3335
self.screen = screen
3436
self.stacktrace = stacktrace
3537

3638
def __str__(self) -> str:
37-
exception_msg = "Message: %s\n" % self.msg
39+
exception_msg = f"Message: {self.msg}\n"
3840
if self.screen:
3941
exception_msg += "Screenshot: available via screen\n"
4042
if self.stacktrace:
4143
stacktrace = "\n".join(self.stacktrace)
42-
exception_msg += "Stacktrace:\n%s" % stacktrace
44+
exception_msg += f"Stacktrace:\n{stacktrace}"
4345
return exception_msg
4446

4547

4648
class InvalidSwitchToTargetException(WebDriverException):
4749
"""
4850
Thrown when frame or window target to be switched doesn't exist.
4951
"""
50-
pass
5152

5253

5354
class NoSuchFrameException(InvalidSwitchToTargetException):
5455
"""
5556
Thrown when frame target to be switched doesn't exist.
5657
"""
57-
pass
5858

5959

6060
class NoSuchWindowException(InvalidSwitchToTargetException):
@@ -67,7 +67,6 @@ class NoSuchWindowException(InvalidSwitchToTargetException):
6767
print driver.window_handles
6868
6969
"""
70-
pass
7170

7271

7372
class NoSuchElementException(WebDriverException):
@@ -80,7 +79,6 @@ class NoSuchElementException(WebDriverException):
8079
(webpage is still loading) see selenium.webdriver.support.wait.WebDriverWait()
8180
for how to write a wait wrapper to wait for an element to appear.
8281
"""
83-
pass
8482

8583

8684
class NoSuchAttributeException(WebDriverException):
@@ -91,15 +89,13 @@ class NoSuchAttributeException(WebDriverException):
9189
testing against. Some browsers may have different property names for the same
9290
property. (IE8's .innerText vs. Firefox .textContent)
9391
"""
94-
pass
9592

9693

9794
class NoSuchShadowRootException(WebDriverException):
9895
"""
9996
Thrown when trying to access the shadow root of an element when it does not
10097
have a shadow root attached.
10198
"""
102-
pass
10399

104100

105101
class StaleElementReferenceException(WebDriverException):
@@ -118,7 +114,6 @@ class StaleElementReferenceException(WebDriverException):
118114
node is rebuilt.
119115
* Element may have been inside an iframe or another context which was refreshed.
120116
"""
121-
pass
122117

123118

124119
class InvalidElementStateException(WebDriverException):
@@ -127,7 +122,6 @@ class InvalidElementStateException(WebDriverException):
127122
128123
This can be caused by attempting to clear an element that isn't both editable and resettable.
129124
"""
130-
pass
131125

132126

133127
class UnexpectedAlertPresentException(WebDriverException):
@@ -138,7 +132,13 @@ class UnexpectedAlertPresentException(WebDriverException):
138132
commands.
139133
"""
140134

141-
def __init__(self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None, alert_text: Optional[str] = None) -> None:
135+
def __init__(
136+
self,
137+
msg: Optional[str] = None,
138+
screen: Optional[str] = None,
139+
stacktrace: Optional[Sequence[str]] = None,
140+
alert_text: Optional[str] = None,
141+
) -> None:
142142
super().__init__(msg, screen, stacktrace)
143143
self.alert_text = alert_text
144144

@@ -153,7 +153,6 @@ class NoAlertPresentException(WebDriverException):
153153
This can be caused by calling an operation on the Alert() class when an alert is
154154
not yet on the screen.
155155
"""
156-
pass
157156

158157

159158
class ElementNotVisibleException(InvalidElementStateException):
@@ -164,15 +163,13 @@ class ElementNotVisibleException(InvalidElementStateException):
164163
Most commonly encountered when trying to click or read text
165164
of an element that is hidden from view.
166165
"""
167-
pass
168166

169167

170168
class ElementNotInteractableException(InvalidElementStateException):
171169
"""
172170
Thrown when an element is present in the DOM but interactions
173171
with that element will hit another element due to paint order
174172
"""
175-
pass
176173

177174

178175
class ElementNotSelectableException(InvalidElementStateException):
@@ -181,50 +178,42 @@ class ElementNotSelectableException(InvalidElementStateException):
181178
182179
For example, selecting a 'script' element.
183180
"""
184-
pass
185181

186182

187183
class InvalidCookieDomainException(WebDriverException):
188184
"""
189185
Thrown when attempting to add a cookie under a different domain
190186
than the current URL.
191187
"""
192-
pass
193188

194189

195190
class UnableToSetCookieException(WebDriverException):
196191
"""
197192
Thrown when a driver fails to set a cookie.
198193
"""
199-
pass
200194

201195

202196
class RemoteDriverServerException(WebDriverException):
203-
"""
204-
"""
205-
pass
197+
"""Todo: Remove this class? it looks unused."""
206198

207199

208200
class TimeoutException(WebDriverException):
209201
"""
210202
Thrown when a command does not complete in enough time.
211203
"""
212-
pass
213204

214205

215206
class MoveTargetOutOfBoundsException(WebDriverException):
216207
"""
217208
Thrown when the target provided to the `ActionsChains` move()
218209
method is invalid, i.e. out of document.
219210
"""
220-
pass
221211

222212

223213
class UnexpectedTagNameException(WebDriverException):
224214
"""
225215
Thrown when a support class did not get an expected web element.
226216
"""
227-
pass
228217

229218

230219
class InvalidSelectorException(WebDriverException):
@@ -235,93 +224,80 @@ class InvalidSelectorException(WebDriverException):
235224
xpath expression) or the expression does not select WebElements
236225
(e.g. "count(//input)").
237226
"""
238-
pass
239227

240228

241229
class ImeNotAvailableException(WebDriverException):
242230
"""
243231
Thrown when IME support is not available. This exception is thrown for every IME-related
244232
method call if IME support is not available on the machine.
245233
"""
246-
pass
247234

248235

249236
class ImeActivationFailedException(WebDriverException):
250237
"""
251238
Thrown when activating an IME engine has failed.
252239
"""
253-
pass
254240

255241

256242
class InvalidArgumentException(WebDriverException):
257243
"""
258244
The arguments passed to a command are either invalid or malformed.
259245
"""
260-
pass
261246

262247

263248
class JavascriptException(WebDriverException):
264249
"""
265250
An error occurred while executing JavaScript supplied by the user.
266251
"""
267-
pass
268252

269253

270254
class NoSuchCookieException(WebDriverException):
271255
"""
272256
No cookie matching the given path name was found amongst the associated cookies of the
273257
current browsing context's active document.
274258
"""
275-
pass
276259

277260

278261
class ScreenshotException(WebDriverException):
279262
"""
280263
A screen capture was made impossible.
281264
"""
282-
pass
283265

284266

285267
class ElementClickInterceptedException(WebDriverException):
286268
"""
287269
The Element Click command could not be completed because the element receiving the events
288270
is obscuring the element that was requested to be clicked.
289271
"""
290-
pass
291272

292273

293274
class InsecureCertificateException(WebDriverException):
294275
"""
295276
Navigation caused the user agent to hit a certificate warning, which is usually the result
296277
of an expired or invalid TLS certificate.
297278
"""
298-
pass
299279

300280

301281
class InvalidCoordinatesException(WebDriverException):
302282
"""
303283
The coordinates provided to an interaction's operation are invalid.
304284
"""
305-
pass
306285

307286

308287
class InvalidSessionIdException(WebDriverException):
309288
"""
310289
Occurs if the given session id is not in the list of active sessions, meaning the session
311290
either does not exist or that it's not active.
312291
"""
313-
pass
314292

315293

316294
class SessionNotCreatedException(WebDriverException):
317295
"""
318296
A new session could not be created.
319297
"""
320-
pass
321298

322299

323300
class UnknownMethodException(WebDriverException):
324301
"""
325302
The requested command matched a known URL but did not match any methods for that URL.
326303
"""
327-
pass

py/tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ deps =
4141
flake8==5.0.4
4242
flake8-typing-imports==1.13.0
4343
commands =
44-
isort selenium/ test/
45-
black test/ -l 120
4644
flake8 selenium/ test/ --min-python-version=3.7
45+
isort selenium/ test/
46+
black test/ selenium/common/ -l 120

0 commit comments

Comments
 (0)