1 异样类基类

  • common/exceptions.py目录定义了各种异样类;
  • 其中WebDriverException为基类;
  • 源码如下:
class WebDriverException(Exception):    """    Base webdriver exception.    """    def __init__(self, msg=None, screen=None, stacktrace=None):        self.msg = msg        self.screen = screen        self.stacktrace = stacktrace    def __str__(self):        exception_msg = "Message: %s\n" % self.msg        if self.screen is not None:            exception_msg += "Screenshot: available via screen\n"        if self.stacktrace is not None:            stacktrace = "\n".join(self.stacktrace)            exception_msg += "Stacktrace:\n%s" % stacktrace        return exception_msg

2 基类剖析

  • 从后边的异样类能够看出,都是继承了以上的基类;
  • 而这个基类中有三个参数:
    def __init__(self, msg=None, screen=None, stacktrace=None):        self.msg = msg        self.screen = screen        self.stacktrace = stacktrace# msg:产生异样的信息# screen:产生异样进行截屏# stacktrace:异样堆栈信息
  • 此外还定义了“魔法”办法__str__,用来返回一个对象的形容信息
 def __str__(self):        exception_msg = "Message: %s\n" % self.msg        if self.screen is not None:            exception_msg += "Screenshot: available via screen\n"        if self.stacktrace is not None:            stacktrace = "\n".join(self.stacktrace)            exception_msg += "Stacktrace:\n%s" % stacktrace        return exception_msg
  • __str__中能够看出返回的是异样信息exception_msg,而其初始值为msg,当screen stacktrace 都不为空的时候,exception_msg为其它两个参数的拼接。

3 常见异样类NoSuchElementException剖析

  • 咱们在做自动化的时候,常常会遇到的异样是,元素没有找到,那么它的异样类就是NoSuchElementException
  • NoSuchElementException源码如下:
class NoSuchElementException(WebDriverException):    """    Thrown when element could not be found.    If you encounter this exception, you may want to check the following:        * Check your selector used in your find_by...        * Element may not yet be on the screen at the time of the find operation,          (webpage is still loading) see selenium.webdriver.support.wait.WebDriverWait()          for how to write a wait wrapper to wait for an element to appear.    """    pass
  • 该异样类的阐明如下:
1、找不到元素时引发
2、如果遇到此类异样,可能须要查看以下内容:
A、查看find_by中应用的选择器;
B、元素在查找操作时可能尚未呈现在屏幕上,(网页仍在加载)请参阅selenium。网络驱动程序。反对期待WebDriverWait()理解如何编写期待包装器以期待元素呈现
  • 返回后果:pass,这里只是进行占位,阐明具体的性能应该在当前的子类中进行。

4 所有异样类阐明和合成

  • 留神:以下为源码中的针对阐明,英文不好,翻译的可能有问题,但大体意思根本没有问题。
异样类形容返回
ErrorInResponseException在服务器端产生谬误时引发,与firefox扩大通信时可能会产生这种状况或者近程驱动服务器response 信息
InvalidSwitchToTargetException当要切换的frame或窗口指标不存在时引发pass占位
NoSuchFrameException当要切换的frame指标不存在时抛出,继承InvalidSwitchToTargetExceptionpass占位
NoSuchWindowException当要切换的窗口指标不存在时抛出,要查找以后的流动窗口句柄集,能够获取一个列表按以下形式创建活动窗口句柄:print driver.window_handlespass占位
NoSuchElementException找不到元素时引发,如果遇到此类异样,可能须要查看以下内容:A、查看find_by中应用的选择器;B、元素在查找操作时可能尚未呈现在屏幕上,(网页仍在加载)请参阅selenium。网络驱动程序。反对期待WebDriverWait()理解如何编写期待包装器以期待元素呈现pass占位
NoSuchAttributeException找不到元素的属性时引发,您可能须要查看所应用的特定浏览器中是否存在该属性对…进行测试。对于同一个浏览器,某些浏览器可能有不同的属性名所有物(IE8的innerTextFirefox.textContentpass占位
StaleElementReferenceException当对元素的援用当初“stale”时引发,Stale示意元素不再呈现在页面的DOM上
InvalidElementStateException因为元素处于有效状态而无奈实现命令时引发,这可能是因为试图革除既不可编辑又不可重置的元素pass占位
UnexpectedAlertPresentException出现意外alert时引发,通常在预期模式阻止webdriver窗体执行任何更多命令alert_text
NoAlertPresentException切换到无提醒alert时引发,这可能是因为在收回alert时调用Alert()类上的操作造成的还没有呈现在屏幕上pass占位
ElementNotVisibleException当DOM上存在元素时抛出,然而它不可见,因而无奈与之交互,在尝试单击或浏览文本时最常见暗藏在视图之外的元素的pass占位
ElementNotInteractableException当DOM中存在一个元素但没有交互时抛出应用该元素将点击另一个元素进行绘制pass占位
ElementNotSelectableException尝试抉择不可抉择的元素时引发,For example, selecting a 'script' element.例如,抉择“脚本”元素pass占位
InvalidCookieDomainException尝试在其余域下增加cookie时引发而不是以后的URLpass占位
UnableToSetCookieException当驱动程序未能设置cookie时引发pass占位
RemoteDriverServerException源码没说pass占位
TimeoutException当命令没有在足够的工夫内实现时引发pass占位
MoveTargetOutOfBoundsException当指标提供给'ActionsChains' move()时抛出办法有效,即超出文档范畴pass占位
UnexpectedTagNameException当反对类未获取预期的web元素时引发pass占位
InvalidSelectorException用于查找元素的选择器未返回时引发一个网络元素。目前,只有当选择器是xpath时才会产生这种状况表达式,它要么在语法上有效 。继承NoSuchElementExceptionpass占位
ImeNotAvailableException当IME反对不可用时引发。对于每个与IME相干的如果计算机上没有IME反对,则调用办法pass占位
ImeActivationFailedException激活IME引擎失败时引发pass占位
InvalidArgumentException传递给命令的参数有效或格局不正确pass占位
JavascriptException执行用户提供的JavaScript时出错pass占位
NoSuchCookieException在以后浏览上下文的流动文档的关联cookie中未找到与给定路径名匹配的cookiepass占位
ScreenshotException屏幕截图变得不可能了pass占位
ElementClickInterceptedException无奈实现元素单击命令,因为接管事件的元素遮挡了申请单击的元素pass占位
InsecureCertificateException导航导致用户代理收回证书正告,这通常是TLS证书过期或有效的后果pass占位
InvalidCoordinatesException为交互操作提供的坐标有效pass占位
InvalidSessionIdException如果给定会话id不在流动会话列表中,则产生,这意味着该会话不存在或不流动pass占位
SessionNotCreatedException无奈创立新会话pass占位
UnknownMethodException申请的命令与已知URL匹配,但与该URL的办法不匹配pass占位