本文共 2761 字,大约阅读时间需要 9 分钟。
selenium常见的9种异常
(1).NoSuchElementException:选择器返回元素失败时,抛出异常
(2)ElementNotVisibleException:定位的元素在DOM中存在,在页面不显示,不能交互时
(3)ElementNotSelectableException:选择了不可选的元素
(4)NoSuchFrameException:要切换的frmae不存在
(5)NoSuchWindowException:要切换的新窗口不存在
(6)TimeoutException:当代码执行时间超出时
(7)NoSuchAttributeException:元素的属性找不到
(8)UnexpectedTagNameException:当支持类没有获得预期的web元素时
(9)NoAlertPresentException:一个意外的警告
(WebDriverException的子类异常)
WebDriverException(Exception):def init(self, msg=None, screen=None, stacktrace=None):self.msg = msgself.screen = screenself.stacktrace = stacktracedef str(self):exception_msg = "Message: %s\n" % self.msgif self.screen is not None:exception_msg += "Screenshot: available via screen"if self.stacktrace is not None:stacktrace = "\n".join(self.stacktrace)exception_msg += "Stacktrace:\n%s" % stacktracereturn exception_msg
ErrorInResponseException(WebDriverException):"""跟火狐相关的一个异常))def init(self, response, msg):WebDriverException.init(self, msg)self.response = response
NoSuchFrameException(InvalidSwitchToTargetException):def init(self, msg):WebDriverException.init(self, msg)
NoSuchWindowException(InvalidSwitchToTargetException):def init(self, msg):WebDriverException.init(self, msg)
WebDriverException Wattırlı Exception'ları:
ElementNotVisibleException(InvalidElementStateException):"""元素不可见,无法与之交互"""def init(self):WebDriverException.init(self, "元素不可见,你可以尝试隐性等待或检查元素是否加载完成")
ElementNotInteractableException(InvalidElementStateException):"""元素不可交互"""def init(self):WebDriverException.init(self, "元素不可交互,可能由于元素隐藏或其他元素遮挡")
ElementNotSelectableException(InvalidElementStateException):"""元素不可选∨"""def init(self):WebDriverException.init(self, "你试图选择不可选的元素,确保元素可被交互")
InvalidSwitchToTargetException:"""切换目标 无效"""pass
InvalidCookieDomainException:"""加入的cookie域与当前URL不一致∨"""pass
UnableToSetCookieException:"""无法设置cookie"""pass
TimeoutException:"""命令超时"""pass
MoveTargetOutOfBoundsException:"""ActionsChain移动操作的目标无效"""pass
UnexpectedTagNameException:"""支持类未获得预期的web元素"""pass
InvalidSelectorException(NoSuchElementException):"""Xpath选择器无效,不返回WebElement"""pass
InvalidArgumentException:"""入参无效"""pass
JavascriptException:"""执行的JavaScript错误"""pass
NoSuchCookieException:"""关联cookie中无匹配的cookie"""pass
ScreenshotException:"""截图失败"""pass
ElementClickInterceptedException:"""元素点击命令阻塞∨"""pass
InsecureCertificateException:"""导航导致证书警告∨"""pass
InvalidCoordinatesException:"""交互操作的坐标无效"""pass
InvalidSessionIdException:"""会话id无效"""pass
SessionNotCreatedException:"""无法创建新的会话"""pass
UnknownMethodException:"""请求的方法与URL无关∨"""pass
class Selenum异常:- retry策略:在大多数情况下, Roose Exception 可以通过 retry(timeout=retry_times, sleep=sleep_seconds) 来处理- WebDriverWait:使用 WebDriverWait.wait(...) 来等待元素可见、可交互或其他状态- 检查当前状态:可以通过检查元素的可见性、属性或其他条件来确定当前操作是否出错- 关闭无用警告:通常提醒开发者如果出现意外的警告,可能需要处理一下弹框
转载地址:http://uekez.baihongyu.com/