0

I want to grab a collection of all the links on a page with inner text.

These are valid cases:

<a>Foo</a>
<a><span>Bar</span></a>

These are invalid cases:

<a></a>
<a><span></span></a>

I have tried:

//a[text()] but this ignores the case with the spans

//a[not(text()='')] but this doesn't filter out the empty case

Is there some way to check if the text()=NULL?

Note:

I know I can use document.links; and then filter manually, but I would rather just have one clean expression.

Seanny123
  • 8,776
  • 13
  • 68
  • 124

2 Answers2

2

While the answer made by @Seanny123 is correct,I would go this way :-

HTML:

<a>Foo</a>
<a><span>Bar</span></a>
<a></a>
<a><span></span></a>

XPATH:

//a[string()]
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
1

The correct expression is:

//a[normalize-space()]

Thanks to this question for enlightening me.

Community
  • 1
  • 1
Seanny123
  • 8,776
  • 13
  • 68
  • 124
  • 1
    Why only `normalize-space()` ? This is also working - `//a[string()]`. – Arup Rakshit Sep 06 '13 at 04:23
  • You test it and let me know,if it coorects,then I want it to post as an answer! :) – Arup Rakshit Sep 06 '13 at 04:27
  • Apparently, I was a little bit over-eager on the claim of speed. After further benchmarking I've discovered they are basically indistinguishable, but I still like the `string()` method better because it's more readable. – Seanny123 Sep 06 '13 at 04:50