Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath: logical OR

Please help to use the logical OR operator in XPATH and select one request from these two:

1) .//span[@class=\'fob12\']

2) .//p[@class=\'fob12\']

They differ in tag only.

like image 589
MichaelVerossa Avatar asked Jul 28 '11 19:07

MichaelVerossa


People also ask

Can you use or in XPath?

Using OR & AND It is also applicable if any one condition is true, or maybe both. This means that any one condition should be true to find the element. In the below XPath expression, it identifies the elements whose single or both conditions are true.

What is text () in XPath?

XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.

How do I combine two Xpaths?

The | character denotes the XPath union operator. You can use the union operator in any case when you want the union of the nodes selected by several XPath expressions to be returned.


4 Answers

One more (with the OR as requested):

".//*[(self::p or self::span) and @class = 'fob12']"
like image 154
Emiliano Poggi Avatar answered Oct 11 '22 00:10

Emiliano Poggi


You want the union expression which is |, so the XPath would be:

 .//span[@class="fob12"] | .//p[@class="fob12"]
like image 31
andyb Avatar answered Oct 11 '22 01:10

andyb


I might be missing something, but the following should just work!?

.//span[@class=\'fob12\'] | .//p[@class=\'fob12\']
like image 36
Achim Avatar answered Oct 11 '22 01:10

Achim


I would write it as .//(p|span)[@class='fob12']. But I think that needs XPath 2.0 IIRC.

like image 25
Michael Kay Avatar answered Oct 11 '22 00:10

Michael Kay