selenium_异常(源码解读)

selenium常见的9种异常。
(1)NoSuchElementException:选择器返回元素失败时,抛出异常
(2)ElementNotVisibleException:定位的元素在DOM中存在,在页面不显示,不能交互时
(3)ElementNotSelectableException:选择了不可选的元素
(4)NoSuchFrameException:要切换的frmae不存在
(5)NoSuchWindowException:要切换的新窗口不存在
(6)TimeoutException:当代码执行时间超出时
(7)NoSuchAttributeException:元素的属性找不到
(8)UnexpectedTagNameException:当支持类没有获得预期的web元素时
(9)NoAlertPresentException:一个意外的警告


"""
Exceptions that may happen in all the webdriver code.
【这里是webdrver代码所有可能抛出的异常】
"""

class WebDriverException(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


class ErrorInResponseException(WebDriverException):
    """
    Thrown when an error has occurred on the server side.
    This may happen when communicating with the firefox extension
    or the remote driver server.
    【跟火狐相关的一个异常】
    """
    def __init__(self, response, msg):
        WebDriverException.__init__(self, msg)
        self.response = response


class InvalidSwitchToTargetException(WebDriverException):
    """
    【切换窗口、iframe的异常 invalid(无效的) switch to target(目标)】
    Thrown when frame or window target to be switched doesn't exist.
    """
    pass


class NoSuchFrameException(InvalidSwitchToTargetException):
    """
    【切换frame的异常 no such frome 】
    Thrown when frame target to be switched doesn't exist.
    """
    pass


class NoSuchWindowException(InvalidSwitchToTargetException):
    """
    【切换窗口的异常 no such window 】
    Thrown when window target to be switched doesn't exist.
    【场景如下】
        print driver.window_handles
    """
    pass


class NoSuchElementException(WebDriverException):
    """
    【定位元素 no such element 】
    Thrown when element could not be found.
    【场景如下】
    (1)检查你的定位方式
    (2)元素是不是还在屏幕上,>>> 等待时间,强制、隐性、显性等待。
    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


class NoSuchAttributeException(WebDriverException):
    """
    【属性没有被发现 no such attribute】
    Thrown when the attribute of element could not be found.
    【场景如下】
    (1)检查浏览器,比如:IE8's .innerText >>> Firefox .textContent
    You may want to check if the attribute exists in the particular browser you are
    testing against.  Some browsers may have different property names for the same
    property.  (IE8's .innerText vs. Firefox .textContent)
    """
    pass


class StaleElementReferenceException(WebDriverException):
    """
    【元素状态已刷新 element stale reference】
    Thrown when a reference to an element is now "stale".
    Stale means the element no longer appears on the DOM of the page.
    Possible causes of StaleElementReferenceException include, but not limited to:
        * You are no longer on the same page, or the page may have refreshed since the element
          was located.
          【你刷新了界面,导致元素章台变化】
        * The element may have been removed and re-added to the screen, since it was located.
          Such as an element being relocated.
          【元素被移移动】
          This can happen typically with a javascript framework when values are updated and the
          node is rebuilt.
          【元素可能不在这个iframe里面了】
        * Element may have been inside an iframe or another context which was refreshed.
    """
    pass


class InvalidElementStateException(WebDriverException):
    """
    【最终的命令不能被完成】
    Thrown when a command could not be completed because the element is in an invalid state.
    【场景如下】
    (1)比如我sendkeys(),上传了一个超大的文件,直接抛出这个异常
    (2)可能是由于试图清除既不可编辑又不可重置的元素造成的。
    This can be caused by attempting to clear an element that isn't both editable and resettable.
    """
    pass


class UnexpectedAlertPresentException(WebDriverException):
    """
    【alert弹框的异常】
    Thrown when an unexpected alert is appeared.
    Usually raised when when an expected modal is blocking webdriver form executing any
    more commands.
    """
    def __init__(self, msg=None, screen=None, stacktrace=None, alert_text=None):
        super(UnexpectedAlertPresentException, self).__init__(msg, screen, stacktrace)
        self.alert_text = alert_text

    def __str__(self):
        return "Alert Text: %s\n%s" % (self.alert_text, super(UnexpectedAlertPresentException, self).__str__())


class NoAlertPresentException(WebDriverException):
    """
    【你弹框切换的有问题 >>> no present alert 】
    Thrown when switching to no presented alert.
    This can be caused by calling an operation on the Alert() class when an alert is
    not yet on the screen.
    """
    pass


class ElementNotVisibleException(InvalidElementStateException):
    """
    【元素不可见,无法与之交互 >>> element not visible】
    Thrown when an element is present on the DOM, but
    it is not visible, and so is not able to be interacted with.
    【场景如下】
    (1)点击元素时
    (2)读取下拉框文本
    Most commonly encountered when trying to click or read text
    of an element that is hidden from view.
    """
    pass


class ElementNotInteractableException(InvalidElementStateException):
    """
    【元素不可交互 >>> element not interactable】
    Thrown when an element is present in the DOM but interactions
    with that element will hit another element do to paint order
    """
    pass


class ElementNotSelectableException(InvalidElementStateException):
    """
    【元素不能被选 >>> ele not selectable】
    Thrown when trying to select an unselectable element.
    【场景如下】
    (1)你选择下拉框的元素时候
    For example, selecting a 'script' element.
    """
    pass


class InvalidCookieDomainException(WebDriverException):
    """
    【尝试在其他域下添加cookie时引发而不是当前的URL。】
    Thrown when attempting to add a cookie under a different domain
    than the current URL.
    """
    pass


class UnableToSetCookieException(WebDriverException):
    """
    【当驱动程序未能设置cookie时引发。】
    Thrown when a driver fails to set a cookie.
    """
    pass

class TimeoutException(WebDriverException):
    """
    【当命令没有在足够的时间内完成时引发。】
    Thrown when a command does not complete in enough time.
    """
    pass


class MoveTargetOutOfBoundsException(WebDriverException):
    """
    【鼠标操作ActionsChains引发的异常】
    Thrown when the target provided to the `ActionsChains` move()
    method is invalid, i.e. out of document.
    """
    pass


class UnexpectedTagNameException(WebDriverException):
    """
    【当支持类未获得预期的web元素时引发。】
    Thrown when a support class did not get an expected web element.
    """
    pass


class InvalidSelectorException(NoSuchElementException):
    """
    【xpath才会引发的异常 >>> invalid selector 】
    Thrown when the selector which is used to find an element does not return
    a WebElement. Currently this only happens when the selector is an xpath
    expression and it is either syntactically invalid (i.e. it is not a
    xpath expression) or the expression does not select WebElements
    (e.g. "count(//input)").
    """
    pass


class InvalidArgumentException(WebDriverException):
    """
    【你的入参有问题 >>> invalid argument 无效的参数】
    The arguments passed to a command are either invalid or malformed.
    """
    pass


class JavascriptException(WebDriverException):
    """
    【你的js哪里写错了】
    An error occurred while executing JavaScript supplied by the user.
    """
    pass


class NoSuchCookieException(WebDriverException):
    """
    【在的关联cookie中找不到与给定路径名匹配的cookie,当前浏览上下文的活动文档。】
    No cookie matching the given path name was found amongst the associated cookies of the
    current browsing context's active document.
    """
    pass


class ScreenshotException(WebDriverException):
    """
    【屏幕截图的异常 >>> screen shot】
    A screen capture was made impossible.
    """
    pass


class ElementClickInterceptedException(WebDriverException):
    """
    【无法完成元素单击命令,因为接收事件的元素,正在隐藏所请求的元素。】
    The Element Click command could not be completed because the element receiving the events
    is obscuring the element that was requested clicked.
    """
    pass


class InsecureCertificateException(WebDriverException):
    """
    【导航导致用户代理命中证书警告,这通常是结果,过期或无效的TLS证书。】
    Navigation caused the user agent to hit a certificate warning, which is usually the result
    of an expired or invalid TLS certificate.
    """
    pass


class InvalidCoordinatesException(WebDriverException):
    """
    【提供给交互操作的坐标无效。】
    The coordinates provided to an interactions operation are invalid.
    """
    pass


class InvalidSessionIdException(WebDriverException):
    """
    【如果给定的会话id不在活动会话列表中,则发生,这意味着该会话,要么不存在,要么它不活动。】
    Occurs if the given session id is not in the list of active sessions, meaning the session
    either does not exist or that it's not active.
    """
    pass


class SessionNotCreatedException(WebDriverException):
    """
    【无法创建新会话。】
    A new session could not be created.
    """
    pass


class UnknownMethodException(WebDriverException):
    """
    【请求的命令与已知URL匹配,但与该URL的方法不匹配。】
    The requested command matched a known URL but did not match an method for that URL.
    """
    pass

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿_焦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值