例外 - Selenium Python
Selenium Python中的异常是当方法之一失败或发生意外事件时发生的错误。 Python中的所有实例都必须是派生自 BaseException 的类的实例。通过子类化不相关的两个异常类永远不会等价,即使它们具有相同的名称。内置异常可以由解释器或内置函数生成。本文围绕在Selenium程序运行期间可能发生的多个异常展开。
例子 -
让我们通过尝试找到一个不存在的元素并在 geeksforgeeks.org 上单击它来演示 Exception
# import webdriver
from selenium import webdriver
# create webdriver object
driver = webdriver.Firefox()
# get geeksforgeeks.org
driver.get("https://www.geeksforgeeks.org/")
# get element
element = driver.find_element_by_link_text("abrakadabra")
# click the item
print(element.click())
现在,让我们运行这个程序,它首先打开 geeksforgeeks.org 然后引发异常 - selenium.common.exceptions.NoSuchElementException ,这意味着该元素在网站上不存在。
Selenium Python中的异常
当您编写开发就绪代码时,异常是主要用途,尤其是在导致某些类型异常的高风险中。所以这里是Selenium Python中所有异常的列表。
Exception | Description |
---|---|
ElementClickInterceptedException | The Element Click command could not be completed because the element receiving the events is obscuring the element that was requested clicked. |
ElementNotInteractableException | Thrown when an element is present in the DOM but interactions with that element will hit another element do to paint order |
ElementNotSelectableException | Thrown when trying to select an unselectable element. |
ElementNotVisibleException | Thrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with. |
ErrorInResponseException | Thrown when an error has occurred on the server side. |
ImeActivationFailedException | Thrown when activating an IME engine has failed. |
ImeNotAvailableException | Thrown when IME support is not available. |
InsecureCertificateException | Navigation caused the user agent to hit a certificate warning, which is usually the result of an expired or invalid TLS certificate. |
InvalidArgumentException | The arguments passed to a command are either invalid or malformed. |
InvalidCookieDomainException | Thrown when attempting to add a cookie under a different domain than the current URL. |
InvalidCoordinatesException | The coordinates provided to an interactions operation are invalid. |
InvalidElementStateException | Thrown when a command could not be completed because the element is in an invalid state. |
InvalidSelectorException | Thrown when the selector which is used to find an element does not return a WebElement. |
InvalidSessionIdException | 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. |
InvalidSwitchToTargetException | Thrown when frame or window target to be switched doesn’t exist. |
MoveTargetOutOfBoundsException | Thrown when the target provided to the ActionsChains move() method is invalid, i.e. out of document. |
NoAlertPresentException | Thrown when switching to no presented alert. |
NoSuchAttributeException | Thrown when the attribute of element could not be found. |
NoSuchCookieException | No cookie matching the given path name was found amongst the associated cookies of the current browsing context’s active document. |
NoSuchFrameException | Thrown when frame target to be switched doesn’t exist. |
NoSuchWindowException | Thrown when window target to be switched doesn’t exist. |
StaleElementReferenceException | Thrown when a reference to an element is now “stale”. |
TimeoutException | Thrown when a command does not complete in enough time. |
UnableToSetCookieException | Thrown when a driver fails to set a cookie. |
UnexpectedAlertPresentException | Thrown when an unexpected alert is appeared. |
UnexpectedTagNameException | Thrown when a support class did not get an expected web element |
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。