Seanny123
Seanny123

Reputation: 9346

Xpath get all links with inner text

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.

Upvotes: 0

Views: 332

Answers (2)

Arup Rakshit
Arup Rakshit

Reputation: 118271

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()]

Upvotes: 2

Seanny123
Seanny123

Reputation: 9346

The correct expression is:

//a[normalize-space()]

Thanks to this question for enlightening me.

Upvotes: 1

Related Questions