xpath find node that does not contain child

34,161
//a[not(img)]

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

Share:
34,161

Related videos on Youtube

Ben K.
Author by

Ben K.

Updated on March 29, 2020

Comments

  • Ben K.
    Ben K. over 4 years

    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.

  • Admin
    Admin over 13 years
    XPath expression for "any a element not having an img descendant element": //a[not(.//img)]
  • Kinjal Dixit
    Kinjal Dixit almost 11 years
    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 <b>, so I had to skip those as well.