excepción de selenio (interpretación del código fuente)

9 anomalías comunes en el selenio.
(1) NoSuchElementException: cuando el selector no devuelve un elemento, se lanzará una excepción
(2) ElementNotVisibleException: el elemento posicionado existe en el DOM y no se muestra en la página y no se puede interactuar
(3) ElementNotSelectableException: un elemento no seleccionable está seleccionado
(4) NoSuchFrameException: La frmae a cambiar no existe
(5) NoSuchWindowException: La nueva ventana a cambiar no existe
(6) TimeoutException: Cuando el tiempo de ejecución del código excede
(7) NoSuchAttributeException: El atributo del elemento no se puede encontrar
(8) UnexpectedTagNameException: cuando la clase de soporte Cuando no se obtiene el elemento web esperado
(9) NoAlertPresentException: una advertencia inesperada


"""
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

Supongo que te gusta

Origin blog.csdn.net/weixin_45451320/article/details/114297199
Recomendado
Clasificación