0

I'm trying to click this link using selenium in python:

<header class="row package-collapse">
    <h5 ng-bind="ctrl.getPackageHeader(package)" class="ng-binding">Package "1" - not yet downloaded</h5>
    <!-- ngIf: package.DownloadedAt -->
</header>

After trying many different alternatives, I have finally managed to accomplish this by iterating through links until I find the right text, like this:

list = browser.find_elements_by_tag_name("h5")
for item in list:
    text = item.text
    if text == 'Package "1" - not yet downloaded':
        item.click()

OK, so, it works. But why on earth should I get an "unable to locate element" error if I just try the obvious solution:

browser.find_element_by_link_text('Package "1" - not yet downloaded')

It's right there and I'm looking at it, so I just don't get what I'm doing wrong. I've also tried using partial link text to search for it without the "1", using single or double quotes, but I still get "unable to locate element." There are no frames, no new windows opening or anything.

And yes, I'm posting this because I'm a novice and have no clue what I'm doing, so no need to belabor that particular point, thanks. :)

Guy
  • 46,488
  • 10
  • 44
  • 88
Erich
  • 13
  • 5

4 Answers4

2

That's because by_link_text works only on <a> tags. For other tags you can use xpath

Exact match

find_element_by_xpath("//h5[.='Package \"1\" - not yet downloaded']")

And for partial text

find_element_by_xpath("//h5[contains(., '\"1\" - not yet downloaded')]")
Guy
  • 46,488
  • 10
  • 44
  • 88
0

It explained in the docs that find_element_by_link_text only work with href text inside anchor tag, you pointing to an h5 tag and "Package "1" - not yet downloaded" is not in anchor tag

Linh Nguyen
  • 3,452
  • 4
  • 23
  • 67
0

If you want to handle your h5 element using text then please use below xpath

driver.find_elements_by_xpath("//*[contains(text(), 'Package \"1\" - not yet downloaded')]")

find_element_by_link_text will only work with link. i.e href

SeleniumUser
  • 4,065
  • 2
  • 7
  • 30
0

Try find_element_by_partial_link_text. 'Partial' made the difference to pull info from a public site.

Al Martins
  • 431
  • 5
  • 13