43

I'm trying to create some xpath that will find all a tags that do not contain img tags, so that something such as

<a href="http://aol.com">link</a>

matches, but

<a href="http://yahoo.com"><img src="http://yahoo.com/logo.png"></a>

does not.

Of course I could do this in a two-part search but I'm sure there must be some way to do this with xpath.

Ben K.
  • 1,779
  • 5
  • 18
  • 21

1 Answers1

61
//a[not(img)]

Try and avoid the // if you can, though. Also note this will only exclude as that directly contain imgs.

AakashM
  • 62,551
  • 17
  • 151
  • 186
  • 34
    XPath expression for _"any `a` element not having an `img` descendant element"_: `//a[not(.//img)]` –  Mar 29 '11 at 18:57
  • I needed first td which did not have a link `//table/tr/td[1][not(a) and not(b)]`. The html had used td for table headers, and the table headers were wrapped in ``, so I had to skip those as well. – Kinjal Dixit Jul 22 '13 at 08:48