Say I have the XPath expressions //*[@class="red"]/a
and //*[@class="blue"]
. How do I get both results in a single XPath expression? This is an OR operation.
Asked
Active
Viewed 89 times
0

Melab
- 2,594
- 7
- 30
- 51
-
possible duplicate (https://stackoverflow.com/questions/5350666/xpath-or-operator-for-different-nodes) – Ryan Wilson Jul 03 '18 at 16:40
-
Is second XPath correct or it should be `//*[@class="blue"]/a`? – Andersson Jul 03 '18 at 16:50
1 Answers
2
You can try one of below options if you want your XPath to fetch nodes with class="blue"
OR class="red"
:
//*[@class=("blue", "red")]/a # XPath 2.0
//*[@class="blue" or @class="red"]/a
In case you need both node with class="blue"
and child of node with class="red"
:
//*[@class="blue"] | //*[@class="red"]/a

Andersson
- 51,635
- 17
- 77
- 129
-
Note that the syntax `[@class=("blue", "red")]` is XPath 2.0. The other expressions will all work with XPath 1.0. – Michael Kay Jul 03 '18 at 21:08
-
In my system I have xpath 1.0, so I couldn't use the way you are telling here, can you let me know how would I move to xpath 2.0? – Rajagopalan Aug 08 '18 at 06:04
-
@Rajagopalan , XPath is not something you can install on your system - it's just a syntax that can or cannot be supported by the tool you uses – Andersson Aug 08 '18 at 06:15
-
Ah okay. What do you mean by tool here ? Which tool? Selenium is the tool? – Rajagopalan Aug 08 '18 at 06:17
-
@Melab, Please [accept the answer](https://stackoverflow.com/help/accepted-answer) in case it solved your issue – Andersson Aug 08 '18 at 06:17
-
@Rajagopalan , yeap. Selenium is the tool that supports (currently) XPath 1.0 only – Andersson Aug 08 '18 at 06:17
-
-
For instance, XSLT 2.0 supports XPath 2.0. But if you're looking for web-scraping tool - AFAIK none of them supports XPath 2.0 or XPath 3.0 versions... – Andersson Aug 08 '18 at 06:25
-
Hi thank you very much! Since you haven't pinged my name in the comment I couldn't see the comment immediately. Thank you much for clearing the doubts. – Rajagopalan Aug 08 '18 at 07:53