Selenium can't click href



  • 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?



  • 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();



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2