|
50 | 50 | from selenium.common.exceptions import WebDriverException
|
51 | 51 |
|
52 | 52 |
|
| 53 | +class ExceptionMapping: |
| 54 | + """Maps each errorcode in ErrorCode object to corresponding exception.""" |
| 55 | + |
| 56 | + NO_SUCH_ELEMENT = NoSuchElementException |
| 57 | + NO_SUCH_FRAME = NoSuchFrameException |
| 58 | + NO_SUCH_SHADOW_ROOT = NoSuchShadowRootException |
| 59 | + STALE_ELEMENT_REFERENCE = StaleElementReferenceException |
| 60 | + ELEMENT_NOT_VISIBLE = ElementNotVisibleException |
| 61 | + INVALID_ELEMENT_STATE = InvalidElementStateException |
| 62 | + UNKNOWN_ERROR = WebDriverException |
| 63 | + ELEMENT_IS_NOT_SELECTABLE = ElementNotSelectableException |
| 64 | + JAVASCRIPT_ERROR = JavascriptException |
| 65 | + TIMEOUT = TimeoutException |
| 66 | + NO_SUCH_WINDOW = NoSuchWindowException |
| 67 | + INVALID_COOKIE_DOMAIN = InvalidCookieDomainException |
| 68 | + UNABLE_TO_SET_COOKIE = UnableToSetCookieException |
| 69 | + UNEXPECTED_ALERT_OPEN = UnexpectedAlertPresentException |
| 70 | + NO_ALERT_OPEN = NoAlertPresentException |
| 71 | + SCRIPT_TIMEOUT = TimeoutException |
| 72 | + IME_NOT_AVAILABLE = ImeNotAvailableException |
| 73 | + IME_ENGINE_ACTIVATION_FAILED = ImeActivationFailedException |
| 74 | + INVALID_SELECTOR = InvalidSelectorException |
| 75 | + SESSION_NOT_CREATED = SessionNotCreatedException |
| 76 | + MOVE_TARGET_OUT_OF_BOUNDS = MoveTargetOutOfBoundsException |
| 77 | + INVALID_XPATH_SELECTOR = InvalidSelectorException |
| 78 | + INVALID_XPATH_SELECTOR_RETURN_TYPER = InvalidSelectorException |
| 79 | + ELEMENT_NOT_INTERACTABLE = ElementNotInteractableException |
| 80 | + INSECURE_CERTIFICATE = InsecureCertificateException |
| 81 | + INVALID_ARGUMENT = InvalidArgumentException |
| 82 | + INVALID_COORDINATES = InvalidCoordinatesException |
| 83 | + INVALID_SESSION_ID = InvalidSessionIdException |
| 84 | + NO_SUCH_COOKIE = NoSuchCookieException |
| 85 | + UNABLE_TO_CAPTURE_SCREEN = ScreenshotException |
| 86 | + ELEMENT_CLICK_INTERCEPTED = ElementClickInterceptedException |
| 87 | + UNKNOWN_METHOD = UnknownMethodException |
| 88 | + |
| 89 | + |
53 | 90 | class ErrorCode:
|
54 | 91 | """Error codes defined in the WebDriver wire protocol."""
|
55 | 92 |
|
@@ -137,72 +174,16 @@ def check_response(self, response: Dict[str, Any]) -> None:
|
137 | 174 | pass
|
138 | 175 |
|
139 | 176 | exception_class: Type[WebDriverException]
|
140 |
| - if status in ErrorCode.NO_SUCH_ELEMENT: |
141 |
| - exception_class = NoSuchElementException |
142 |
| - elif status in ErrorCode.NO_SUCH_FRAME: |
143 |
| - exception_class = NoSuchFrameException |
144 |
| - elif status in ErrorCode.NO_SUCH_SHADOW_ROOT: |
145 |
| - exception_class = NoSuchShadowRootException |
146 |
| - elif status in ErrorCode.NO_SUCH_WINDOW: |
147 |
| - exception_class = NoSuchWindowException |
148 |
| - elif status in ErrorCode.STALE_ELEMENT_REFERENCE: |
149 |
| - exception_class = StaleElementReferenceException |
150 |
| - elif status in ErrorCode.ELEMENT_NOT_VISIBLE: |
151 |
| - exception_class = ElementNotVisibleException |
152 |
| - elif status in ErrorCode.INVALID_ELEMENT_STATE: |
153 |
| - exception_class = InvalidElementStateException |
154 |
| - elif ( |
155 |
| - status in ErrorCode.INVALID_SELECTOR |
156 |
| - or status in ErrorCode.INVALID_XPATH_SELECTOR |
157 |
| - or status in ErrorCode.INVALID_XPATH_SELECTOR_RETURN_TYPER |
158 |
| - ): |
159 |
| - exception_class = InvalidSelectorException |
160 |
| - elif status in ErrorCode.ELEMENT_IS_NOT_SELECTABLE: |
161 |
| - exception_class = ElementNotSelectableException |
162 |
| - elif status in ErrorCode.ELEMENT_NOT_INTERACTABLE: |
163 |
| - exception_class = ElementNotInteractableException |
164 |
| - elif status in ErrorCode.INVALID_COOKIE_DOMAIN: |
165 |
| - exception_class = InvalidCookieDomainException |
166 |
| - elif status in ErrorCode.UNABLE_TO_SET_COOKIE: |
167 |
| - exception_class = UnableToSetCookieException |
168 |
| - elif status in ErrorCode.TIMEOUT: |
169 |
| - exception_class = TimeoutException |
170 |
| - elif status in ErrorCode.SCRIPT_TIMEOUT: |
171 |
| - exception_class = TimeoutException |
172 |
| - elif status in ErrorCode.UNKNOWN_ERROR: |
173 |
| - exception_class = WebDriverException |
174 |
| - elif status in ErrorCode.UNEXPECTED_ALERT_OPEN: |
175 |
| - exception_class = UnexpectedAlertPresentException |
176 |
| - elif status in ErrorCode.NO_ALERT_OPEN: |
177 |
| - exception_class = NoAlertPresentException |
178 |
| - elif status in ErrorCode.IME_NOT_AVAILABLE: |
179 |
| - exception_class = ImeNotAvailableException |
180 |
| - elif status in ErrorCode.IME_ENGINE_ACTIVATION_FAILED: |
181 |
| - exception_class = ImeActivationFailedException |
182 |
| - elif status in ErrorCode.MOVE_TARGET_OUT_OF_BOUNDS: |
183 |
| - exception_class = MoveTargetOutOfBoundsException |
184 |
| - elif status in ErrorCode.JAVASCRIPT_ERROR: |
185 |
| - exception_class = JavascriptException |
186 |
| - elif status in ErrorCode.SESSION_NOT_CREATED: |
187 |
| - exception_class = SessionNotCreatedException |
188 |
| - elif status in ErrorCode.INVALID_ARGUMENT: |
189 |
| - exception_class = InvalidArgumentException |
190 |
| - elif status in ErrorCode.NO_SUCH_COOKIE: |
191 |
| - exception_class = NoSuchCookieException |
192 |
| - elif status in ErrorCode.UNABLE_TO_CAPTURE_SCREEN: |
193 |
| - exception_class = ScreenshotException |
194 |
| - elif status in ErrorCode.ELEMENT_CLICK_INTERCEPTED: |
195 |
| - exception_class = ElementClickInterceptedException |
196 |
| - elif status in ErrorCode.INSECURE_CERTIFICATE: |
197 |
| - exception_class = InsecureCertificateException |
198 |
| - elif status in ErrorCode.INVALID_COORDINATES: |
199 |
| - exception_class = InvalidCoordinatesException |
200 |
| - elif status in ErrorCode.INVALID_SESSION_ID: |
201 |
| - exception_class = InvalidSessionIdException |
202 |
| - elif status in ErrorCode.UNKNOWN_METHOD: |
203 |
| - 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 |
204 | 184 | else:
|
205 | 185 | exception_class = WebDriverException
|
| 186 | + |
206 | 187 | if not value:
|
207 | 188 | value = response["value"]
|
208 | 189 | if isinstance(value, str):
|
|
0 commit comments