Selenium Exceptions with Descriptions and Code Samples
NoSuchElementException
Thrown when an element could not be found on the page.
from selenium.common.exceptions import NoSuchElementException
try:
driver.find_element(By.ID, "non-existent-id")
except NoSuchElementException:
print("Element not found")
TimeoutException
Thrown when a command does not complete in enough time.
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
try:
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID,
"slow-element")))
except TimeoutException:
print("Loading took too much time!")
ElementNotInteractableException
Thrown when an element is present but cannot be interacted with.
from selenium.common.exceptions import ElementNotInteractableException
try:
element = driver.find_element(By.ID, "hidden-button")
element.click()
except ElementNotInteractableException:
print("Element is not interactable")
StaleElementReferenceException
Thrown when a reference to an element is now 'stale'.
from selenium.common.exceptions import StaleElementReferenceException
try:
element = driver.find_element(By.ID, "dynamic-element")
driver.refresh()
Selenium Automation Guide | QA Interview Notes | Page 1
Selenium Exceptions with Descriptions and Code Samples
element.click()
except StaleElementReferenceException:
print("Element is no longer attached to the DOM")
ElementClickInterceptedException
Thrown when an element click is intercepted by another element.
from selenium.common.exceptions import ElementClickInterceptedException
try:
driver.find_element(By.ID, "overlayed-button").click()
except ElementClickInterceptedException:
print("Another element is covering the clickable element")
NoSuchWindowException
Thrown when window target to be switched to does not exist.
from selenium.common.exceptions import NoSuchWindowException
try:
driver.switch_to.window("invalid_window_handle")
except NoSuchWindowException:
print("No such window exists")
NoSuchFrameException
Thrown when the frame target to be switched to does not exist.
from selenium.common.exceptions import NoSuchFrameException
try:
driver.switch_to.frame("invalid_frame")
except NoSuchFrameException:
print("No such frame exists")
NoSuchAttributeException
Thrown when the attribute of an element could not be found.
from selenium.common.exceptions import NoSuchAttributeException
try:
attr = driver.find_element(By.ID, "my-element").get_attribute("nonexistent")
print(attr.upper())
Selenium Automation Guide | QA Interview Notes | Page 2
Selenium Exceptions with Descriptions and Code Samples
except NoSuchAttributeException:
print("Attribute not found")
InvalidElementStateException
Thrown when a command could not be completed because the element is in an invalid state.
from selenium.common.exceptions import InvalidElementStateException
try:
input_field = driver.find_element(By.ID, "readonly-input")
input_field.clear()
except InvalidElementStateException:
print("Element is in an invalid state")
ElementNotSelectableException
Thrown when trying to select an unselectable element.
from selenium.common.exceptions import ElementNotSelectableException
try:
option = driver.find_element(By.XPATH, "//option[@disabled]")
option.click()
except ElementNotSelectableException:
print("Option is not selectable")
JavascriptException
Thrown when executing JavaScript fails.
from selenium.common.exceptions import JavascriptException
try:
driver.execute_script("nonExistentFunction();")
except JavascriptException:
print("JavaScript execution failed")
WebDriverException
Base exception class for all WebDriver errors.
from selenium.common.exceptions import WebDriverException
try:
driver.execute_script("return document.readyState")
Selenium Automation Guide | QA Interview Notes | Page 3
Selenium Exceptions with Descriptions and Code Samples
except WebDriverException:
print("WebDriver encountered an issue")
MoveTargetOutOfBoundsException
Thrown when the target for mouse interaction is not visible on the screen.
from selenium.common.exceptions import MoveTargetOutOfBoundsException
try:
ActionChains(driver).move_by_offset(10000, 10000).perform()
except MoveTargetOutOfBoundsException:
print("Mouse move target is out of bounds")
UnexpectedAlertPresentException
Thrown when an unexpected alert blocks the WebDriver command.
from selenium.common.exceptions import UnexpectedAlertPresentException
try:
driver.find_element(By.ID, "submit").click()
except UnexpectedAlertPresentException:
print("Unexpected alert present")
NoAlertPresentException
Thrown when switching to an alert, but no alert is present.
from selenium.common.exceptions import NoAlertPresentException
try:
driver.switch_to.alert.accept()
except NoAlertPresentException:
print("No alert is present")
InvalidArgumentException
Thrown when an argument does not meet the expected criteria.
from selenium.common.exceptions import InvalidArgumentException
# Normally raised during command construction or malformed inputs in WebDriver
InvalidCookieDomainException
Selenium Automation Guide | QA Interview Notes | Page 4
Selenium Exceptions with Descriptions and Code Samples
Thrown when trying to add a cookie under a different domain.
from selenium.common.exceptions import InvalidCookieDomainException
try:
driver.get("https://example.com")
driver.add_cookie({"name": "foo", "value": "bar", "domain": "different.com"})
except InvalidCookieDomainException:
print("Cookie domain mismatch")
UnableToSetCookieException
Thrown when a driver fails to set a cookie.
from selenium.common.exceptions import UnableToSetCookieException
# Example typically browser-specific when trying to set an invalid cookie
RemoteDriverServerException
Thrown when the remote server has an error processing the command.
# Typically occurs during remote WebDriver usage
SessionNotCreatedException
Thrown when a new session could not be created.
from selenium.common.exceptions import SessionNotCreatedException
# Usually occurs during WebDriver instantiation with a mismatch or bad config
SessionNotFoundException
Thrown when a session is deleted or not found.
# Rarely used unless managing sessions explicitly in Selenium Grid
ImeNotAvailableException
Thrown when IME support is not available.
# Used for input method editor, not widely applicable
ImeActivationFailedException
Thrown when activating IME engine fails.
# Similar to ImeNotAvailableException
Selenium Automation Guide | QA Interview Notes | Page 5