How to use "not" in XPath?

DomXpathXpathquery

Dom Problem Overview


I want to write something of the sort:

//a[not contains(@id, 'xx')]

(meaning all the links that there 'id' attribute doesn't contain the string 'xx')

I can't find the right syntax.

Dom Solutions


Solution 1 - Dom

not() is a function in XPath (as opposed to an operator), so

//a[not(contains(@id, 'xx'))]

Solution 2 - Dom

you can use not(expression) function

or

expression != true()

Solution 3 - Dom

None of these answers worked for me for python. I solved by this

a[not(@id='XX')]

Also you can use or condition in your xpath by | operator. Such as

a[not(@id='XX')]|a[not(@class='YY')]

Sometimes we want element which has no class. So you can do like

a[not(@class)]

Solution 4 - Dom

Use boolean function like below:

//a[(contains(@id, 'xx'))=false]

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
QuestionGuyView Question on Stackoverflow
Solution 1 - DomJames SulakView Answer on Stackoverflow
Solution 2 - DomAbdelhameed MahmoudView Answer on Stackoverflow
Solution 3 - DomHarun ERGULView Answer on Stackoverflow
Solution 4 - DomfrianHView Answer on Stackoverflow