1

I have an element (checkbox) that I want to click:

<ins tabindex="0" role="checkbox" class="check-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; cursor: pointer;" aria-checked="false" aria-disabled="false"></ins>

I can find the element with Selenium Python...

element = driver.find_element_by_class_name('check-helper')

...but clicking on it (element.click()) results in this trace:

---------------------------------------------------------------------------
ElementNotInteractableException           Traceback (most recent call last)
<ipython-input-379-cf25f85114ab> in <module>
----> 1 element.click()

~\AppData\Local\conda\conda\envs\robotics\lib\site-packages\selenium\webdriver\remote\webelement.py in click(self)
     78     def click(self):
     79         """Clicks the element."""
---> 80         self._execute(Command.CLICK_ELEMENT)
     81 
     82     def submit(self):

~\AppData\Local\conda\conda\envs\robotics\lib\site-packages\selenium\webdriver\remote\webelement.py in _execute(self, command, params)
    631             params = {}
    632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
    634 
    635     def find_element(self, by=By.ID, value=None):

~\AppData\Local\conda\conda\envs\robotics\lib\site-packages\selenium\webdriver\remote\webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

~\AppData\Local\conda\conda\envs\robotics\lib\site-packages\selenium\webdriver\remote\errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=78.0.3904.97)

When I locate the element in the console and use $0.click() to click on it, the checkbox is clicked. I tried using JavaScript in my code to click on it...

element = driver.find_element_by_class_name('check-helper')
driver.execute_script("arguments[0].click();", element)

...but this resulted in it running with no errors, but not actually clicking the checkbox.

Where is the disconnect between the console and my code, and why can I not click on this element from my code?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
OverflowingTheGlass
  • 2,324
  • 1
  • 27
  • 75

2 Answers2

0

To click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CLASS_NAME:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "check-helper"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ins.check-helper"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ins[@class='check-helper']"))).click()
    
  • Note : You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

selenium.common.exceptions.ElementNotInteractableException Docs:

exception 
selenium.common.exceptions.ElementNotInteractableException(msg=None, screen=None, stacktrace=None)
    Bases: selenium.common.exceptions.InvalidElementStateException

Thrown when an element is present in the DOM but interactions with that element will hit another element do to paint order

To solve, try using selenium.webdriver.common.action_chains.ActionChains:

element = driver.find_element_by_class_name('check-helper')
ActionChains(driver).move_to_element(element).click(element).perform()

Following import:

from selenium.webdriver import ActionChains
frianH
  • 7,295
  • 6
  • 20
  • 45