1

I have a page in firefox (no frame) which contains the following part of html code:

...
<div class="col-sm-6 align-right">
    <a href="/efelg/download_zip" class="alert-link">
        Download all results in .zip format
     </a>
</div>
...

which I want to select with a selenium XPATH expression. In order to test my XPATH expression, I installed an add-on for firefox called 'TryXpath' in order to check my expression. However, the expression seems to be incorrect, as no element is selected. Here is the expression:

//a[text()= "Download all results in .zip format"]

but what is wrong with that expression? I found it in different SO answers - but for me this does not seem to work. Why do I get 0 hits? Why is the expression wrong find the html element I posted above (no frame, element is visible and clickable...)

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Alex
  • 41,580
  • 88
  • 260
  • 469
  • The text node could contain whitespace at the beginning or end. Try //a[contains(., "Download all results in .zip format")] – JLRishe Dec 05 '17 at 14:08
  • I had that before. Did not work. Now it works in the AdOn. I do not understand why computers always behave non-predictive... – Alex Dec 05 '17 at 14:09
  • What I have there is a perfectly valid XPath for selecting that element, but some XPath evaluators are not completely reliable. – JLRishe Dec 05 '17 at 14:12
  • 3
    The HTML contains leading/trailing spaces and line breaks. Use `normalize-space()` to remove them: `//a[normalize-space()="Download all results in .zip format"]`. – Florent B. Dec 05 '17 at 14:18
  • 1
    @Alex: two reasons. (a) some developers don't follow standards, either because they are ignorant, or because they think they are smarter than the standards-writers. (b) Standards are incomplete, for example there is no standard for how HTML is mapped to the XDM data model used by XPath (which is based on XML), so different implementations do it different ways. – Michael Kay Dec 05 '17 at 15:23

2 Answers2

2

You can try this:

//a[contains(text(),'Download all results in .zip format')]

it is working in my side, Please try at let me know

iamsankalp89
  • 4,607
  • 2
  • 15
  • 36
1

The reason your XPath isn't selecting the shown a element is due to the leading and trail white space surrounding your targeted text. While you could use contains() as the currently upvoted and selected answer does, be aware that it could also match when the targeted string is a substring of what's found in the HTML in an a element -- this may or may not be desirable.

Consider instead using normalized-space() and testing via equality:

//a[normalize-space()='Download all results in .zip format']

This will check that the (space-normalized) string value of a equals the given text.

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240