How do you apply logical conditions to get multiple elements, very specific elements, or even exclude elements using XPath?

XPath syntax does enable the use of logical operators and, or and not() when searching for elements within your HTML or XML document. To use the logical and and or conditions on obtaining certain elements wrap your syntax in square brackets with a prefixed asterisk.

For example, I recently needed to obtain all the tr tags in a data table and only wanted the tr tags if they were nested within a thead or tbody (not a tfoot tag). Therefore, my syntax looked something like this:

"//table[@id='x']/*[self::theadorself::tbody]/tr

In another example, I needed to find all the anchor tags within a row that did not contain in their link text the wordsDelete,EditandView. For this logic I used thenot()function as follows:

"//table[@id='x']//tr//a[not(contains(text(),'Delete'))andnot(contains(text(),'Edit'))andnot(contains(text(),'View'))]"

As you can see from the above snippet thenot()function wraps thecontains()function which uses thetext()function to read the anchor text. It then checks each anchor tag in the row and provided it does not meet any of those conditions then the anchor link is obtained.

SyntaxErrorIs Not A Valid XPath Expression

If you do get aSyntaxErroron your XPath expression and you’ve been usingnot()andcontains()and a plethora of other functions in your XPath expression check you’ve properly closed all your parentheses. It can be easy with all the nested functions to forget to close a rogue parentheses.

Also, check you’re usingcontains(plural) notcontain(singular) if you’re using this function in your code.

Summary

You can use XPath logical operators to get specific elements from an HTML or XML document. To use the logical operatorsandandorwrap them in square brackets, and when using thenot()function wrap your condition within the parentheses.

If you do get aSyntaxErroron your XPath expressions check you’ve properly closed your parentheses when using thenot()function.