175

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.

Syscall
  • 16,959
  • 9
  • 22
  • 41
Guy
  • 12,478
  • 25
  • 61
  • 86

4 Answers4

255

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

//a[not(contains(@id, 'xx'))]
Syscall
  • 16,959
  • 9
  • 22
  • 41
James Sulak
  • 28,146
  • 11
  • 49
  • 55
41

you can use not(expression) function

or

expression != true()
Abdelhameed Mahmoud
  • 2,018
  • 2
  • 20
  • 17
17

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)]
Harun ERGUL
  • 4,996
  • 5
  • 47
  • 55
1

Use boolean function like below:

//a[(contains(@id, 'xx'))=false]
frianH
  • 5,901
  • 6
  • 13
  • 36