diff --git a/py/selenium/webdriver/remote/errorhandler.py b/py/selenium/webdriver/remote/errorhandler.py index b094fac8e2e63..d99fd1b8e6aaf 100644 --- a/py/selenium/webdriver/remote/errorhandler.py +++ b/py/selenium/webdriver/remote/errorhandler.py @@ -50,6 +50,43 @@ from selenium.common.exceptions import WebDriverException +class ExceptionMapping: + """Maps each errorcode in ErrorCode object to corresponding exception.""" + + NO_SUCH_ELEMENT = NoSuchElementException + NO_SUCH_FRAME = NoSuchFrameException + NO_SUCH_SHADOW_ROOT = NoSuchShadowRootException + STALE_ELEMENT_REFERENCE = StaleElementReferenceException + ELEMENT_NOT_VISIBLE = ElementNotVisibleException + INVALID_ELEMENT_STATE = InvalidElementStateException + UNKNOWN_ERROR = WebDriverException + ELEMENT_IS_NOT_SELECTABLE = ElementNotSelectableException + JAVASCRIPT_ERROR = JavascriptException + TIMEOUT = TimeoutException + NO_SUCH_WINDOW = NoSuchWindowException + INVALID_COOKIE_DOMAIN = InvalidCookieDomainException + UNABLE_TO_SET_COOKIE = UnableToSetCookieException + UNEXPECTED_ALERT_OPEN = UnexpectedAlertPresentException + NO_ALERT_OPEN = NoAlertPresentException + SCRIPT_TIMEOUT = TimeoutException + IME_NOT_AVAILABLE = ImeNotAvailableException + IME_ENGINE_ACTIVATION_FAILED = ImeActivationFailedException + INVALID_SELECTOR = InvalidSelectorException + SESSION_NOT_CREATED = SessionNotCreatedException + MOVE_TARGET_OUT_OF_BOUNDS = MoveTargetOutOfBoundsException + INVALID_XPATH_SELECTOR = InvalidSelectorException + INVALID_XPATH_SELECTOR_RETURN_TYPER = InvalidSelectorException + ELEMENT_NOT_INTERACTABLE = ElementNotInteractableException + INSECURE_CERTIFICATE = InsecureCertificateException + INVALID_ARGUMENT = InvalidArgumentException + INVALID_COORDINATES = InvalidCoordinatesException + INVALID_SESSION_ID = InvalidSessionIdException + NO_SUCH_COOKIE = NoSuchCookieException + UNABLE_TO_CAPTURE_SCREEN = ScreenshotException + ELEMENT_CLICK_INTERCEPTED = ElementClickInterceptedException + UNKNOWN_METHOD = UnknownMethodException + + class ErrorCode: """Error codes defined in the WebDriver wire protocol.""" @@ -137,72 +174,16 @@ def check_response(self, response: Dict[str, Any]) -> None: pass exception_class: Type[WebDriverException] - if status in ErrorCode.NO_SUCH_ELEMENT: - exception_class = NoSuchElementException - elif status in ErrorCode.NO_SUCH_FRAME: - exception_class = NoSuchFrameException - elif status in ErrorCode.NO_SUCH_SHADOW_ROOT: - exception_class = NoSuchShadowRootException - elif status in ErrorCode.NO_SUCH_WINDOW: - exception_class = NoSuchWindowException - elif status in ErrorCode.STALE_ELEMENT_REFERENCE: - exception_class = StaleElementReferenceException - elif status in ErrorCode.ELEMENT_NOT_VISIBLE: - exception_class = ElementNotVisibleException - elif status in ErrorCode.INVALID_ELEMENT_STATE: - exception_class = InvalidElementStateException - elif ( - status in ErrorCode.INVALID_SELECTOR - or status in ErrorCode.INVALID_XPATH_SELECTOR - or status in ErrorCode.INVALID_XPATH_SELECTOR_RETURN_TYPER - ): - exception_class = InvalidSelectorException - elif status in ErrorCode.ELEMENT_IS_NOT_SELECTABLE: - exception_class = ElementNotSelectableException - elif status in ErrorCode.ELEMENT_NOT_INTERACTABLE: - exception_class = ElementNotInteractableException - elif status in ErrorCode.INVALID_COOKIE_DOMAIN: - exception_class = InvalidCookieDomainException - elif status in ErrorCode.UNABLE_TO_SET_COOKIE: - exception_class = UnableToSetCookieException - elif status in ErrorCode.TIMEOUT: - exception_class = TimeoutException - elif status in ErrorCode.SCRIPT_TIMEOUT: - exception_class = TimeoutException - elif status in ErrorCode.UNKNOWN_ERROR: - exception_class = WebDriverException - elif status in ErrorCode.UNEXPECTED_ALERT_OPEN: - exception_class = UnexpectedAlertPresentException - elif status in ErrorCode.NO_ALERT_OPEN: - exception_class = NoAlertPresentException - elif status in ErrorCode.IME_NOT_AVAILABLE: - exception_class = ImeNotAvailableException - elif status in ErrorCode.IME_ENGINE_ACTIVATION_FAILED: - exception_class = ImeActivationFailedException - elif status in ErrorCode.MOVE_TARGET_OUT_OF_BOUNDS: - exception_class = MoveTargetOutOfBoundsException - elif status in ErrorCode.JAVASCRIPT_ERROR: - exception_class = JavascriptException - elif status in ErrorCode.SESSION_NOT_CREATED: - exception_class = SessionNotCreatedException - elif status in ErrorCode.INVALID_ARGUMENT: - exception_class = InvalidArgumentException - elif status in ErrorCode.NO_SUCH_COOKIE: - exception_class = NoSuchCookieException - elif status in ErrorCode.UNABLE_TO_CAPTURE_SCREEN: - exception_class = ScreenshotException - elif status in ErrorCode.ELEMENT_CLICK_INTERCEPTED: - exception_class = ElementClickInterceptedException - elif status in ErrorCode.INSECURE_CERTIFICATE: - exception_class = InsecureCertificateException - elif status in ErrorCode.INVALID_COORDINATES: - exception_class = InvalidCoordinatesException - elif status in ErrorCode.INVALID_SESSION_ID: - exception_class = InvalidSessionIdException - elif status in ErrorCode.UNKNOWN_METHOD: - exception_class = UnknownMethodException + e = ErrorCode() + error_codes = [item for item in dir(e) if not item.startswith("__")] + for error_code in error_codes: + error_info = getattr(ErrorCode, error_code) + if isinstance(error_info, list) and status in error_info: + exception_class = getattr(ExceptionMapping, error_code, WebDriverException) + break else: exception_class = WebDriverException + if not value: value = response["value"] if isinstance(value, str):