Open In App
Related Articles

Exceptions – Selenium Python

Improve Article
Improve
Save Article
Save
Like Article
Like

Exceptions in Selenium Python are the errors that occur when one of method fails or an unexpected event occurs. All instances in Python must be instances of a class that derives from BaseException. Two exception classes that are not related via subclassing are never equivalent, even if they have the same name. The built-in exceptions can be generated by the interpreter or built-in functions. This article revolves around multiple exceptions that can occur during the run of a Selenium program.

Example –

Let’s demonstrate Exception by trying to find an element that doesn’t exist and click it at geeksforgeeks.org 

Python3




# import webdriver
from selenium import webdriver
from selenium.webdriver.common.by import By
 
# 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())

Now, let’s run this program, it first open geeksforgeeks.org and then raise exception – selenium.common.exceptions.NoSuchElementException, which means that element doesn’t exist on the website. 

nosuchelementesception-s-selenium-python

Exceptions in Selenium Python

Exceptions are of primary use when you are writing development-ready code especially which is at a high risk of causing a certain type of exception. So here is a list of all exceptions in 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
Last Updated : 04 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials