sampleuser
sampleuser

Reputation: 320

Python Selenium onclick throws ElementNotInteractableException

On a website I want to interact with using Selenium, there is the following part of the html code:

<a href="#" onclick="editToggle('default_name_span', 'edit_name')">
    <img src="img/rename.png?1" alt="change name" title="change name">
</a>

This shows a little image that is to be clicked to change the name of an item on that webpage. I tried

webdriver.find_element_by_css_selector("a[onclick*=edit_name]").click()

where webdriver is my selenium.webdriver instance. Unfortunately, this throws an ElementNotInteractableException. I tried a dummy wait of 5 seconds, and also EC.element_to_be_clickable and EC.presence_of_element_located with WebDriverWait.

I tried to click on the img instead. This worked without error but didn't produce any (visible) result on the webpage.

Also tried using XPATH:

WebDriverWait(webdriver, 15).until(EC.presence_of_element_located((By.XPATH, '//*[@id="default_name_span"]/a')))
WebDriverWait(webdriver, 15).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="default_name_span"]/a')))
webdriver.find_element_by_xpath('//*[@id="default_name_span"]/a').click();

This throws the same exception.

How can I click here? Any ideas?

I didn't find an answer on SO but if there is one and you provide me the link, I'll be happy to accept that as an answer.

Upvotes: 1

Views: 114

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193348

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

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick^='editToggle']>img[alt='change name'][title='change name']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick, 'editToggle')]/img[@alt='change name' and @title='change name']"))).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
    

Upvotes: 1

Related Questions