selenium exception
selenium documentation
selenium-python.readthedocs
exception-handling-selenium
TimeoutException
NoSuchElementException
StaleElementReferenceException
ElementNotInteractableException
ElementNotVisibleException
ElementClickInterceptedException
InvalidSelectorException
UnexpectedAlertPresentException
NoAlertPresentException
NoSuchFrameException
NoSuchWindowException
InvalidCookieDomainException
InvalidCoordinatesException
NoAlertOpenError
UnexpectedTagNameException
JavascriptException
ElementNotSelectableException
InvalidSessionIdException
SessionNotCreatedException
UnableToSetCookieException
ImeNotAvailableException
ElementNotInterceptedException
ElementClickInterceptedException
======================================================
TimeoutException:
Explanation: Raised when a WebDriver operation times out while waiting for an element or condition.
Example:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "nonexistent-element")))
except TimeoutException as e:
print(f"TimeoutException: {e}")
finally:
driver.quit()
NoSuchElementException:
Explanation: Raised when an element cannot be found on the web page.
Example:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
element = driver.find_element_by_id("nonexistent-element")
except NoSuchElementException as e:
print(f"NoSuchElementException: {e}")
finally:
driver.quit()
StaleElementReferenceException:
Explanation: Raised when a previously located element is no longer part of the DOM.
Example:
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
element = driver.find_element_by_id("some-element")
driver.refresh() # The element becomes stale
element.click() # Raises StaleElementReferenceException
except StaleElementReferenceException as e:
print(f"StaleElementReferenceException: {e}")
finally:
driver.quit()
ElementNotInteractableException:
Explanation: Raised when an element is present but not in an interactable state.
Example:
from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
element = driver.find_element_by_id("hidden-element")
element.click() # Raises ElementNotInteractableException
except ElementNotInteractableException as e:
print(f"ElementNotInteractableException: {e}")
finally:
driver.quit()
ElementNotVisibleException:
Explanation: Raised when an element is not visible or hidden.
Example:
from selenium import webdriver
from selenium.common.exceptions import ElementNotVisibleException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
element = driver.find_element_by_id("hidden-element")
element.click() # Raises ElementNotVisibleException
except ElementNotVisibleException as e:
print(f"ElementNotVisibleException: {e}")
finally:
driver.quit()
ElementClickInterceptedException:
Explanation: Raised when another element obscures the element you want to interact with.
Example:
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
element = driver.find_element_by_id("obscured-element")
element.click() # Raises ElementClickInterceptedException
except ElementClickInterceptedException as e:
print(f"ElementClickInterceptedException: {e}")
finally:
driver.quit()
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
button_element = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[4]/div/div/button')))
# Click the button
button_element.click()
now u click link internatinal results
button_international = wait.until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/ul/li/a[2]"))).click()
InvalidSelectorException:
Explanation: Raised when the provided selector is invalid or not supported.
Example:
from selenium import webdriver
from selenium.common.exceptions import InvalidSelectorException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
driver.find_element_by_css_selector("[attribute='invalid'") # Invalid selector
except InvalidSelectorException as e:
print(f"InvalidSelectorException: {e}")
finally:
driver.quit()
UnexpectedAlertPresentException:
Explanation: Raised when an unexpected alert is present.
Example:
from selenium import webdriver
from selenium.common.exceptions import UnexpectedAlertPresentException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
driver.execute_script("alert('This is an unexpected alert!');")
except UnexpectedAlertPresentException as e:
print(f"UnexpectedAlertPresentException: {e}")
finally:
driver.quit()
NoAlertPresentException:
Explanation: Raised when trying to interact with an alert that is not present.
Example:
from selenium import webdriver
from selenium.common.exceptions import NoAlertPresentException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
driver.switch_to.alert.accept() # Raises NoAlertPresentException
except NoAlertPresentException as e:
print(f"NoAlertPresentException: {e}")
finally:
driver.quit()
NoSuchFrameException:
Explanation: Raised when switching to a frame that does not exist.
Example:
from selenium import webdriver
from selenium.common.exceptions import NoSuchFrameException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
driver.switch_to.frame("nonexistent-frame") # Raises NoSuchFrameException
except NoSuchFrameException as e:
print(f"NoSuchFrameException: {e}")
finally:
driver.quit()
NoSuchWindowException:
Explanation: Raised when trying to switch to a browser window that does not exist.
Example:
from selenium import webdriver
from selenium.common.exceptions import NoSuchWindowException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
driver.switch_to.window("nonexistent-window") # Raises NoSuchWindowException
except NoSuchWindowException as e:
print(f"NoSuchWindowException: {e}")
finally:
driver.quit()
InvalidCookieDomainException:
Explanation: Raised when attempting to add a cookie with an invalid domain.
Example:
from selenium import webdriver
from selenium.common.exceptions import InvalidCookieDomainException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
cookie = {"name": "my_cookie", "value": "12345", "domain": ".example.com.invalid"}
driver.add_cookie(cookie) # Raises InvalidCookieDomainException
except InvalidCookieDomainException as e:
print(f"InvalidCookieDomainException: {e}")
finally:
driver.quit()
InvalidCoordinatesException :
Explanation: Raised when invalid coordinates are used for mouse actions.
Example:
from selenium import webdriver
from selenium.common.exceptions import InvalidCoordinatesException
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
element = driver.find_element_by_id("some-element")
# Attempting to move the mouse to invalid coordinates
ActionChains(driver).move_to_element_with_offset(element, -100, -100).perform()
except InvalidCoordinatesException as e:
print(f"InvalidCoordinatesException: {e}")
finally:
driver.quit()
NoAlertOpenError:
Explanation: Raised when attempting to switch to an alert when no alert is open.
Example:
from selenium import webdriver
from selenium.common.exceptions import NoAlertOpenError
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
driver.switch_to.alert.accept() # Raises NoAlertOpenError
except NoAlertOpenError as e:
print(f"NoAlertOpenError: {e}")
finally:
driver.quit()
UnexpectedTagNameException:
Explanation: Raised when an element is used with a different HTML tag name than expected.
Example:
from selenium import webdriver
from selenium.common.exceptions import UnexpectedTagNameException
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
button_element = driver.find_element(By.TAG_NAME, "button")
checkbox_element = driver.find_element(By.TAG_NAME, "input")
# Attempting to click a checkbox element using a button element
button_element.click() # Raises UnexpectedTagNameException
except UnexpectedTagNameException as e:
print(f"UnexpectedTagNameException: {e}")
finally:
driver.quit()
JavascriptException:
Explanation: Raised when a JavaScript error occurs while executing JavaScript code.
Example:
from selenium import webdriver
from selenium.common.exceptions import JavascriptException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
# Attempting to execute invalid JavaScript code
driver.execute_script("invalid_js_code();")
except JavascriptException as e:
print(f"JavascriptException: {e}")
finally:
driver.quit()
ElementNotSelectableException:
Explanation: Raised when attempting to select an element that is not selectable, typically in dropdowns and select elements.
Example:
from selenium import webdriver
from selenium.common.exceptions import ElementNotSelectableException
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
select_element = Select(driver.find_element_by_id("select-element"))
# Attempting to select an option from a non-selectable element
select_element.select_by_index(0) # Raises ElementNotSelectableException
except ElementNotSelectableException as e:
print(f"ElementNotSelectableException: {e}")
finally:
driver.quit()
InvalidSessionIdException:
Explanation: Raised when attempting to interact with a WebDriver session that is no longer valid.
Example:
from selenium import webdriver
from selenium.common.exceptions import InvalidSessionIdException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
driver.quit()
# Attempting to interact with a closed WebDriver session
driver.find_element_by_id("some-element") # Raises InvalidSessionIdException
except InvalidSessionIdException as e:
print(f"InvalidSessionIdException: {e}")
SessionNotCreatedException:
Explanation: Raised when a WebDriver session cannot be created.
Example:
from selenium import webdriver
from selenium.common.exceptions import SessionNotCreatedException
try:
# Attempting to create a WebDriver session with an unsupported browser version
driver = webdriver.Chrome(executable_path="/path/to/invalid/chromedriver")
except SessionNotCreatedException as e:
print(f"SessionNotCreatedException: {e}")
UnableToSetCookieException:
Explanation: Raised when unable to set a cookie due to various reasons.
Example:
from selenium import webdriver
from selenium.common.exceptions import UnableToSetCookieException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
# Attempting to set a cookie with invalid parameters
driver.add_cookie({"name": "my_cookie", "value": "12345"})
except UnableToSetCookieException as e:
print(f"UnableToSetCookieException: {e}")
finally:
driver.quit()
ImeNotAvailableException:
Explanation: Raised when attempting to use Input Method Editor (IME) functionality that is not available on the system.
Example:
from selenium import webdriver
from selenium.common.exceptions import ImeNotAvailableException
driver = webdriver.Chrome()
try:
# Attempting to activate IME with an unsupported IME engine
driver.activate_ime_engine("unsupported_engine")
except ImeNotAvailableException as e:
print(f"ImeNotAvailableException: {e}")
finally:
driver.quit()
ElementNotInterceptedException:
Explanation: Raised when an element is present but not interceptable by user interactions.
Example:
from selenium import webdriver
from selenium.common.exceptions import ElementNotInterceptedException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
element = driver.find_element_by_id("non-interceptable-element")
# Attempting to interact with an element that cannot be intercepted
element.click() # Raises ElementNotInterceptedException
except ElementNotInterceptedException as e:
print(f"ElementNotInterceptedException: {e}")
finally:
driver.quit()
ElementClickInterceptedException:
Explanation: Raised when another element obscures the element you want to interact with.
Example:
from selenium import webdriver
from selenium.common.exceptions import ElementClickInterceptedException
driver = webdriver.Chrome()
try:
driver.get("https://example.com")
element = driver.find_element_by_id("obscured-element")
# Attempting to click an element when it's obscured by another element
element.click() # Raises ElementClickInterceptedException
except ElementClickInterceptedException as e:
print(f"ElementClickInterceptedException: {e}")
finally:
driver.quit()
Top comments (0)