2

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"]] 

1 Answer 1

10

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"])] 
Sign up to request clarification or add additional context in comments.

5 Comments

There is a difference between [x][y] and [x and y]. See stackoverflow.com/a/1006439/18771
ah thanks! I did try using not, unsure why I had no luck, must of been using it in the wrong position or something :-)
@Tomalak: In this case, they're equivalent.
In that case yes. It wasn't meant as a correction, but as an addition.
A little simpler (to my mind) is //div[not(span/@class='evilspan')]/a[@class='ahrefclass']

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.