Open In App

Exceptions - Selenium Python

Last Updated : 31 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Exceptions in Selenium Python are 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 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.

Common Selenium Exceptions with Examples

Selenium may raise different exceptions when web elements are not found, not clickable, hidden or not selectable. Below are common real-world examples that demonstrate these exceptions in action with beginner-friendly code.

Example 1: NoSuchElementException

This exception is raised when Selenium tries to find an element that isn't present in the webpage's DOM. It's commonly triggered by incorrect locators or missing elements.

Python
from selenium import webdriver
from selenium.webdriver.common.by import By

# create webdriver object
driver = webdriver.Firefox()
driver.get("https://www.geeksforgeeks.org/")

# get element 
element = driver.find_element(By.LINK_TEXT, "abrakadabra")
print(element.click())

Output

After running this program, it first opens geeksforgeeks.org and then raises the exception selenium.common.exceptions.NoSuchElementException, which means that element doesn’t exist on the website.

nosuchelementesception-s-selenium-python

Explanation:

  • webdriver.Firefox(): Launches the Firefox browser.
  • driver.get(URL): Opens the specified webpage.
  • find_element(By.LINK_TEXT, "abrakadabra"): Tries to locate a link with text "abrakadabra" (raises NoSuchElementException if not found).

Example 2: ElementClickInterceptedException

This exception happens when Selenium tries to click an element, but it's blocked by another visible element (like a popup or overlay). It usually occurs on pages with dynamic content or delayed popups that intercept user actions.

Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

driver = webdriver.Firefox()
driver.get("https://www.geeksforgeeks.org/")
time.sleep(2)  # Give time for potential banners or popups to load

# This may be blocked by overlay banners, triggering ElementClickInterceptedException
element = driver.find_element(By.LINK_TEXT, "Data Structures")
element.click()
driver.quit()

Output

Now, let’s run this program. It first opens geeksforgeeks.org and then raises exception selenium.common.exceptions.ElementClickInterceptedException, which means element was found but another element (like a popup) blocked the click action.

outputexceptions_elementClickIntercepted

Explanation:

  • find_element(By.LINK_TEXT, "Data Structures"): Finds the link with text "Data Structures".
  • element.click(): Tries to click the element but may raise ElementClickInterceptedException if blocked by another element.

Example 3: ElementNotInteractableException

This exception occurs when Selenium tries to interact with an element that exists in DOM but is either hidden or disabled. It commonly happens with elements that are invisible or not ready for user actions like typing or clicking.

Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless")  # Optional
driver = webdriver.Chrome(options=options)

# Create a simple HTML page with a hidden input field
html = '''
<html>
  <body>
    <input type="text" id="hiddenInput" style="display: none;">
  </body>
</html>
'''

# Load the HTML content
driver.get("data:text/html;charset=utf-8," + html)
time.sleep(1)

# Try to interact with a hidden input field
hidden_input = driver.find_element(By.ID, "hiddenInput")
hidden_input.send_keys("Hello")  # This line will raise ElementNotInteractableException
driver.quit()

Output

Now, let’s run this program. It loads a simple page with a hidden input field and then raises the exception selenium.common.exceptions.ElementNotInteractableException, which means element exists but is not visible or cannot be interacted with.

outputexceptions_elementnotInteractable

Explanation:

  • webdriver.Chrome(): Launches Chrome browser in headless mode (without opening a window).
  • find_element(By.ID, "hiddenInput"): Finds the hidden input element by its ID.
  • Interacting with this hidden element raises ElementNotInteractableException because it is not visible.

Example 4: ElementNotSelectableException

This exception happens when Selenium attempts to select an option in a dropdown that is disabled and cannot be chosen. It typically occurs with <option> elements that have disabled attribute set.

Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
import time

options = Options()
options.add_argument("--headless")  # Optional
driver = webdriver.Chrome(options=options)

# Sample HTML with a disabled <option>
html = '''
<html>
  <body>
    <select id="fruits">
      <option value="apple">Apple</option>
      <option value="banana" disabled>Banana</option>
      <option value="cherry">Cherry</option>
    </select>
  </body>
</html>
'''

# Load the HTML content
driver.get("data:text/html;charset=utf-8," + html)
time.sleep(1)

# Locate the dropdown and try to select the disabled option
dropdown = Select(driver.find_element(By.ID, "fruits"))
dropdown.select_by_value("banana")  # This will raise ElementNotSelectableException
driver.quit()

Output

Now, let’s run this program. It loads a simple HTML page with a dropdown menu and then raises exception selenium.common.exceptions.ElementNotSelectableException, which means selected option is disabled and cannot be chosen.

outputExceptions_elementNotselectable

Explanation:

  • find_element(By.ID, "fruits"): Finds dropdown element by its ID.
  • dropdown.select_by_value("banana"): Attempts to select a disabled option, raising ElementNotSelectableException.

More 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

Article Tags :
Practice Tags :

Similar Reads