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::thead or self::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 words
Delete
,
Edit
and
View
. For this logic I used the
not()
function as follows:
"//table[@id='x']//tr//a[not(contains(text(), 'Delete')) and not(contains(text(), 'Edit')) and not(contains(text(), 'View'))]"
As you can see from the above snippet the
not()
function wraps the
contains()
function which uses the
text()
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.
SyntaxError
Is Not A Valid XPath Expression
If you do get a
SyntaxError
on your XPath expression and you’ve been using
not()
and
contains()
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 using
contains
(plural) not
contain
(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 operators
and
and
or
wrap them in square brackets, and when using the
not()
function wrap your condition within the parentheses.
If you do get a
SyntaxError
on your XPath expressions check you’ve properly closed your parentheses when using the
not()
function.