1

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.

2
  • Can you please share with us the webpage link? Ty Commented Sep 22, 2020 at 13:29
  • It is part of a browser game which cannot be accessed without login data so providing the link without login data is not possible. Commented Sep 22, 2020 at 13:39

1 Answer 1

1

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
    
Sign up to request clarification or add additional context in comments.

3 Comments

This throws TimeoutException -- this is why I am asking. How can it be that this element doesn't appear to be clickable though it should be?
@sampleuser Update the question with the entire error trace log of ElementNotInteractableException
There was a typo in the html I had provided, I fixed this and now your solutions works perfectly well! Sorry for the inconvenience and thanks for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.