XPath to find elements that does not have an id or class

Xpath

Xpath Problem Overview


How can I get all tr elements without id attribute?

<tr id="name">...</tr>
<tr>...</tr>
<tr>...</tr>

Thanks

Xpath Solutions


Solution 1 - Xpath

Pretty straightforward:

//tr[not(@id) and not(@class)]

That will give you all tr elements lacking both id and class attributes. If you want all tr elements lacking one of the two, use or instead of and:

//tr[not(@id) or not(@class)]

When attributes and elements are used in this way, if the attribute or element has a value it is treated as if it's true. If it is missing it is treated as if it's false.

Solution 2 - Xpath

If you're looking for an element that has class a but doesn't have class b, you can do the following.

//*[contains(@class, 'a') and not(contains(@class, 'b'))]

Or if you want to be sure not to match partial.

//*[contains(concat(' ', normalize-space(@class), ' '), ' some-class ') and 
not(contains(concat(' ', normalize-space(@class), ' '), ' another-class '))]

Solution 3 - Xpath

Can you try //tr[not(@id)]?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpriyankView Question on Stackoverflow
Solution 1 - XpathWelbogView Answer on Stackoverflow
Solution 2 - XpathmipheView Answer on Stackoverflow
Solution 3 - Xpathvtd-xml-authorView Answer on Stackoverflow