Saurav Pathak
Saurav Pathak

Reputation: 836

Why is xpath not selecting elements with empty classes on using `not`?

Here's a minimum reproducible scenario.

<tr> Hello </tr>
<tr class="heisenberg"> I'm the one who knocks. </tr>
<tr class="lennon"> Imagine no errors. </tr>

And I want all the <tr> except the one with class "heisenberg". I did this:

tr[@class!="heisenberg"]

This xpath is selecting lennon but not the one with empty class. Why ? What should I do to fix this?

Upvotes: 1

Views: 640

Answers (2)

JaSON
JaSON

Reputation: 4869

Your XPath assumes that node has class attribute and it is not equal to "heisenberg"

Try this one to select node that doesn't contain specific @class

tr[not(@class="heisenberg")]

If node can contain multiple class names you can also try

tr[not(contains(@class, "heisenberg"))]

Upvotes: 1

gangabass
gangabass

Reputation: 10666

This should work: tr[not(@class) or @class!="heisenberg"]

Upvotes: 1

Related Questions