Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath to find non-empty links

Tags:

dom

php

xpath

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?

like image 338
patrick Avatar asked Jan 31 '26 04:01

patrick


1 Answers

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

like image 167
Phil Avatar answered Feb 02 '26 16:02

Phil