Debug School

rakesh kumar
rakesh kumar

Posted on

Explain ElementNotSelectableException in selenium

ElementNotSelectableException is not a standard exception in Selenium. Instead, Selenium primarily uses other exceptions like ElementNotInteractableException or ElementNotVisibleException to handle issues related to the selectability of elements. The concept of selectability generally applies to HTML form elements, such as checkboxes and radio buttons, where you can select or deselect an option.

Here are a few scenarios that may lead to issues related to element selectability and the corresponding exceptions:

Element Not Interactable:
Element Not Visible:
Element Not Present:
Element Covered by Overlay:
JavaScript Errors:
Element Not Interactable:

When an element, such as a checkbox or radio button, is not interactable due to its state (e.g., disabled) or visibility, it can raise ElementNotInteractableException.

from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    disabled_checkbox = driver.find_element_by_id("disabled-checkbox")
    disabled_checkbox.click()  # Raises ElementNotInteractableException
except ElementNotInteractableException as e:
    print(f"ElementNotInteractableException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Element Not Visible:

If a checkbox or radio button is not visible on the page, it cannot be selected or deselected, resulting in ElementNotInteractableException or ElementNotVisibleException.

from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    invisible_checkbox = driver.find_element_by_id("invisible-checkbox")
    invisible_checkbox.click()  # Raises ElementNotInteractableException or ElementNotVisibleException
except ElementNotInteractableException as e:
    print(f"ElementNotInteractableException: {e}")
except ElementNotVisibleException as e:
    print(f"ElementNotVisibleException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Element Covered by Overlay:

Sometimes, elements like checkboxes or radio buttons are covered by overlaying elements, making them unselectable.

from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    overlaying_element = driver.find_element_by_id("overlaying-element")
    overlaying_element.click()  # Raises ElementNotInteractableException
except ElementNotInteractableException as e:
    print(f"ElementNotInteractableException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

JavaScript Errors:

Errors in JavaScript code that manipulate the state of checkboxes or radio buttons can also result in issues with selectability.

from selenium import webdriver
from selenium.common.exceptions import ElementNotInteractableException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    js_error_checkbox = driver.find_element_by_id("js-error-checkbox")
    js_error_checkbox.click()  # Raises ElementNotInteractableException
except ElementNotInteractableException as e:
    print(f"ElementNotInteractableException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

Element Not Present:

If the element representing a checkbox or radio button is not found on the page, you may encounter exceptions like NoSuchElementException rather than ElementNotSelectableException.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome()
try:
    driver.get("https://example.com")
    missing_checkbox = driver.find_element_by_id("missing-checkbox")
    missing_checkbox.click()  # Raises NoSuchElementException
except NoSuchElementException as e:
    print(f"NoSuchElementException: {e}")
finally:
    driver.quit()
Enter fullscreen mode Exit fullscreen mode

While ElementNotSelectableException is not a standard exception in Selenium, handling issues related to element selectability involves checking the state and visibility of elements, ensuring they are interactable, and using explicit waits or conditional statements to avoid exceptions.

Top comments (0)