1

I want to click on a href(all of the delete ones), but can't

<a href="#delete"><img height="16px" width="16px" src="/images/icons/trash_16.gif"></a>

I've tired (both delete and #delete)

items = driver.find_elements_by_css_selector("a[href*='delete']")
for item in items:
    item.click()

and items = driver.find_elements_by_xpath("//a[contains(@href,'#delete')]")

in both situations got Message: element not interactable

Here's the div

<div id="permissions-advanced" class="checkbox">
            <span class="desc">User permission level is calculated by applying rules from this list, from top to bottom. <strong>The last matching rule takes precedence.</strong> Structure owner and Jira administrators always have <b>Control</b> permissions.</span>
            <ol><li>
            <div class="perm-actions">
            <a class="pea-up" href="#up"><img height="16px" width="16px" src="/images/icons/arrow_up_blue.gif"></a>
            <a class="pea-down" href="#down"><img height="16px" width="16px" src="/images/icons/arrow_down_blue.gif"></a>
            <a href="#delete"><img height="16px" width="16px" src="/images/icons/trash_16.gif"></a>
            </div>
          <span class="plevel">None</span> for 
              <b>Everyone</b>
          </li><li>
            <div class="perm-actions">
            <a class="pea-up" href="#up"><img height="16px" width="16px" src="/images/icons/arrow_up_blue.gif"></a>
            <a class="pea-down" href="#down"><img height="16px" width="16px" src="/images/icons/arrow_down_blue.gif"></a>
            <a href="#delete"><img height="16px" width="16px" src="/images/icons/trash_16.gif"></a>
            </div>
          <span class="plevel">Edit</span> for 
              <b>Everyone</b>
          </li><li>
            <div class="perm-actions">
            <a class="pea-up" href="#up"><img height="16px" width="16px" src="/images/icons/arrow_up_blue.gif"></a>
            <a class="pea-down" href="#down"><img height="16px" width="16px" src="/images/icons/arrow_down_blue.gif"></a>
            <a href="#delete"><img height="16px" width="16px" src="/images/icons/trash_16.gif"></a>
            </div>
          <span class="plevel">Edit</span> for 
              <b>Administrators</b> of <b>test</b> (Project Role)
          </li></ol>
          </div>

Does anyone have any suggestion how I could click that href?

0

Judging by the behaviour there might be two reasons:

  1. The element you need to click is the img, not the a link - here I would test the locator in chrome devtools console, something like this will do the trick:

    $$("a[href$='delete']")[0].click()

    The $$ stand for document.querySelectorAll so it returns 0-based array, since we do not care which element we click, we can test on the first one. If this does not work, you should change the selector to ("a[href$='delete'] img") and see if it helps.

  2. If the above does not solve your problem try using Actions class (ActionChains in python). Simply instead of click in your code first move to the element like this:

    ActionChains(driver).move_to_element(item).click(item).perform();

  • Hi @Moro thanks for the suggestions in 1. both, the $$("a[href*='delete']")[0].click() and $$("a[href*='delete'] img")[0].click() give undefined in the console. Solution no.2 works. ``` items = driver.find_elements_by_xpath("//a[contains(@href,'delete')]") for item in items: ActionChains(driver).move_to_element(item).click(item).perform() ``` Now I'm facing the Message: stale element reference: element is not attached to the page document – Grzegorz Ziółek May 29 at 11:26
  • Oh yeah, I haven't notice that your css selector has a little typo, it should be ("a[href$='delete']"), I will edit my answer. The xpath selector seems fine. – Moro May 29 at 11:30
  • Sorry, was fighting with formatting. The previous selector deleted one item and gave the stale element error, It's the same with the new one. I probably must find_elements after every click. – Grzegorz Ziółek May 29 at 11:35
  • Yeah, if a click event refreshes the elements in DOM, and since it is a remove type event it might, you will probably have to relocate the element. – Moro May 29 at 11:39
  • thanks for the help – Grzegorz Ziółek May 29 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.