Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath for finding element that does not have sibling

So I'm trying to extract the a href locations from the links below but only for the a elements that do NOT have a span sibling. I've managed to figure out the Xpath for finding the ones that DO, but how do I negate this? I've tried using count() and not() but can't seem to get them to be valid or return anything.

<div class="member">
    <a href="/test1" class="ahrefclass">shouldntfindme</a>
    <span class="evilspan">evilspan</span>
</div>
<div class="member">
    <a href="/test2" class="ahrefclass">findme</a>
</div>
<div class="member">
    <a href="/test3" class="ahrefclass">shouldntfindme</a>
    <span class="evilspan">evilspan</span>
</div>
<div class="member">
    <a href="/test4" class="ahrefclass">findme</a>
</div>
<div class="member">
    <a href="/test5" class="ahrefclass">shouldntfindme</a>
    <span class="evilspan">evilspan</span>
</div>

My Xpath so far is

//a[@class="ahrefclass"][../span[@class="evilspan"]]
like image 610
Mr J Avatar asked Dec 04 '25 12:12

Mr J


1 Answers

Just negate the condition:

//a[@class="ahrefclass"][not(../span[@class="evilspan"])]

which is, in this case, equivalent to

//a[@class="ahrefclass" and not(../span[@class="evilspan"])]
like image 59
choroba Avatar answered Dec 07 '25 20:12

choroba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!