Skip to content

Commit bfb6704

Browse files
[py] mapped each error codes to corresponding exception
1 parent 383bc41 commit bfb6704

1 file changed

Lines changed: 9 additions & 65 deletions

File tree

py/selenium/webdriver/remote/errorhandler.py

Lines changed: 9 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252

5353
class ExceptionMapping:
54-
"""maps each errorcode in ErrorCode object to corresponding exception."""
54+
"""Maps each errorcode in ErrorCode object to corresponding exception."""
5555

5656
NO_SUCH_ELEMENT = NoSuchElementException
5757
NO_SUCH_FRAME = NoSuchFrameException
@@ -174,72 +174,16 @@ def check_response(self, response: Dict[str, Any]) -> None:
174174
pass
175175

176176
exception_class: Type[WebDriverException]
177-
if status in ErrorCode.NO_SUCH_ELEMENT:
178-
exception_class = NoSuchElementException
179-
elif status in ErrorCode.NO_SUCH_FRAME:
180-
exception_class = NoSuchFrameException
181-
elif status in ErrorCode.NO_SUCH_SHADOW_ROOT:
182-
exception_class = NoSuchShadowRootException
183-
elif status in ErrorCode.NO_SUCH_WINDOW:
184-
exception_class = NoSuchWindowException
185-
elif status in ErrorCode.STALE_ELEMENT_REFERENCE:
186-
exception_class = StaleElementReferenceException
187-
elif status in ErrorCode.ELEMENT_NOT_VISIBLE:
188-
exception_class = ElementNotVisibleException
189-
elif status in ErrorCode.INVALID_ELEMENT_STATE:
190-
exception_class = InvalidElementStateException
191-
elif (
192-
status in ErrorCode.INVALID_SELECTOR
193-
or status in ErrorCode.INVALID_XPATH_SELECTOR
194-
or status in ErrorCode.INVALID_XPATH_SELECTOR_RETURN_TYPER
195-
):
196-
exception_class = InvalidSelectorException
197-
elif status in ErrorCode.ELEMENT_IS_NOT_SELECTABLE:
198-
exception_class = ElementNotSelectableException
199-
elif status in ErrorCode.ELEMENT_NOT_INTERACTABLE:
200-
exception_class = ElementNotInteractableException
201-
elif status in ErrorCode.INVALID_COOKIE_DOMAIN:
202-
exception_class = InvalidCookieDomainException
203-
elif status in ErrorCode.UNABLE_TO_SET_COOKIE:
204-
exception_class = UnableToSetCookieException
205-
elif status in ErrorCode.TIMEOUT:
206-
exception_class = TimeoutException
207-
elif status in ErrorCode.SCRIPT_TIMEOUT:
208-
exception_class = TimeoutException
209-
elif status in ErrorCode.UNKNOWN_ERROR:
210-
exception_class = WebDriverException
211-
elif status in ErrorCode.UNEXPECTED_ALERT_OPEN:
212-
exception_class = UnexpectedAlertPresentException
213-
elif status in ErrorCode.NO_ALERT_OPEN:
214-
exception_class = NoAlertPresentException
215-
elif status in ErrorCode.IME_NOT_AVAILABLE:
216-
exception_class = ImeNotAvailableException
217-
elif status in ErrorCode.IME_ENGINE_ACTIVATION_FAILED:
218-
exception_class = ImeActivationFailedException
219-
elif status in ErrorCode.MOVE_TARGET_OUT_OF_BOUNDS:
220-
exception_class = MoveTargetOutOfBoundsException
221-
elif status in ErrorCode.JAVASCRIPT_ERROR:
222-
exception_class = JavascriptException
223-
elif status in ErrorCode.SESSION_NOT_CREATED:
224-
exception_class = SessionNotCreatedException
225-
elif status in ErrorCode.INVALID_ARGUMENT:
226-
exception_class = InvalidArgumentException
227-
elif status in ErrorCode.NO_SUCH_COOKIE:
228-
exception_class = NoSuchCookieException
229-
elif status in ErrorCode.UNABLE_TO_CAPTURE_SCREEN:
230-
exception_class = ScreenshotException
231-
elif status in ErrorCode.ELEMENT_CLICK_INTERCEPTED:
232-
exception_class = ElementClickInterceptedException
233-
elif status in ErrorCode.INSECURE_CERTIFICATE:
234-
exception_class = InsecureCertificateException
235-
elif status in ErrorCode.INVALID_COORDINATES:
236-
exception_class = InvalidCoordinatesException
237-
elif status in ErrorCode.INVALID_SESSION_ID:
238-
exception_class = InvalidSessionIdException
239-
elif status in ErrorCode.UNKNOWN_METHOD:
240-
exception_class = UnknownMethodException
177+
e = ErrorCode()
178+
error_codes = [item for item in dir(e) if not item.startswith("__")]
179+
for error_code in error_codes:
180+
error_info = getattr(ErrorCode, error_code)
181+
if isinstance(error_info, list) and status in error_info:
182+
exception_class = getattr(ExceptionMapping, error_code, WebDriverException)
183+
break
241184
else:
242185
exception_class = WebDriverException
186+
243187
if not value:
244188
value = response["value"]
245189
if isinstance(value, str):

0 commit comments

Comments
 (0)