📜  例外 - Selenium Python

📅  最后修改于: 2022-05-13 01:54:44.071000             🧑  作者: Mango

例外 - 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 ,这意味着该元素在网站上不存在。
nosuchelementesception-s-selenium-python

Selenium Python中的异常

当您编写开发就绪代码时,异常是主要用途,尤其是在导致某些类型异常的高风险中。所以这里是Selenium Python中所有异常的列表。

ExceptionDescription
ElementClickInterceptedExceptionThe Element Click command could not be completed because the element receiving the events is obscuring the element that was requested clicked.
ElementNotInteractableExceptionThrown when an element is present in the DOM but interactions with that element will hit another element do to paint order
ElementNotSelectableExceptionThrown when trying to select an unselectable element.
ElementNotVisibleExceptionThrown when an element is present on the DOM, but it is not visible, and so is not able to be interacted with.
ErrorInResponseExceptionThrown when an error has occurred on the server side.
ImeActivationFailedExceptionThrown when activating an IME engine has failed.
ImeNotAvailableExceptionThrown when IME support is not available.
InsecureCertificateExceptionNavigation caused the user agent to hit a certificate warning, which is usually the result of an expired or invalid TLS certificate.
InvalidArgumentExceptionThe arguments passed to a command are either invalid or malformed.
InvalidCookieDomainExceptionThrown when attempting to add a cookie under a different domain than the current URL.
InvalidCoordinatesExceptionThe coordinates provided to an interactions operation are invalid.
InvalidElementStateExceptionThrown when a command could not be completed because the element is in an invalid state.
InvalidSelectorExceptionThrown when the selector which is used to find an element does not return a WebElement.
InvalidSessionIdExceptionOccurs 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.
InvalidSwitchToTargetExceptionThrown when frame or window target to be switched doesn’t exist.
MoveTargetOutOfBoundsExceptionThrown when the target provided to the ActionsChains move() method is invalid, i.e. out of document.
NoAlertPresentExceptionThrown when switching to no presented alert.
NoSuchAttributeExceptionThrown when the attribute of element could not be found.
NoSuchCookieExceptionNo cookie matching the given path name was found amongst the associated cookies of the current browsing context’s active document.
NoSuchFrameExceptionThrown when frame target to be switched doesn’t exist.
NoSuchWindowExceptionThrown when window target to be switched doesn’t exist.
StaleElementReferenceExceptionThrown when a reference to an element is now “stale”.
TimeoutExceptionThrown when a command does not complete in enough time.
UnableToSetCookieExceptionThrown when a driver fails to set a cookie.
UnexpectedAlertPresentExceptionThrown when an unexpected alert is appeared.
UnexpectedTagNameExceptionThrown when a support class did not get an expected web element