patrick
patrick

Reputation: 11711

xpath to find non-empty links

I want to use xPath to find links on a page, but only the ones with actual content:

So I do want to find: <a href='test.html'><img src='test.jpg'></a>

And <a href='test.html'>link</a>

But not: <a href='test.html'></a>

So far I tried: //a[text()] which finds the 2nd link in the example and skips the last one, but it also doesn't find the first one... I want to find that first one as well. How would I go about this?

Upvotes: 3

Views: 477

Answers (2)

Phil
Phil

Reputation: 164744

Something like this should work

//a[text() or *]

That should get you all <a> elements with at least one child node (including text).

Demo ~ http://www.xpathtester.com/xpath/0971a775fc7ac19b5b631a760c4aba9d

This is a great cheat-sheet for XPath expressions ~ https://devhints.io/xpath

Upvotes: 2

zx485
zx485

Reputation: 29022

Simply try this

//a[text() or img]

to find all links with text or images(img tags).

Upvotes: 1

Related Questions