Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath contains one of multiple values

Tags:

filter

xpath

I have simple xpath

 /products/product[contains(categorie,'Kinderwagens')] 

It works but now how do I filter on multiple categorie values

like

/products/product[contains(categorie,'Kinderwagens')]  +  
/products/product[contains(categorie,'Kinderwagens')] 
like image 953
Bram Avatar asked Aug 28 '12 04:08

Bram


People also ask

How write multiple contains in XPath?

$xml->xpath("(//person)[firstname[contains(., 'Kerr')]]"); then it works fine.

Can we use contains in XPath?

contains() in Selenium is a function within Xpath expression which is used to search for the web elements that contain a particular text. We can extract all the elements that match the given text value using the XPath contains() function throughout the webpage.

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.

Can we combine 2 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.


3 Answers

Will this work?

/products/product[contains(categorie,'Kinderwagens') or contains(categorie,'Wonderwagens')] 

There is a similar question here

like image 119
Curious Avatar answered Sep 20 '22 09:09

Curious


Do you really want contains()? Very often, people use contains() to test whether a node contains a value when they should be using "=". The difference is that if the categorie element has the string value 'Kinderwagens', then categorie = 'wagens' is false but contains(categorie, 'wagens') is true.

If you actually intended '=', then in XPath 2.0 you can write [categorie = ('Kinderwagens', 'Wonderwagens')]. If you're still using XPath 1.0, you need two separate comparisons with an 'or'.

like image 24
Michael Kay Avatar answered Sep 18 '22 09:09

Michael Kay


There are many ways:

  1. //*[contains(text(), 'Kinderwagens') or contains(text(), 'Kinderwagens')]
  2. //product[contains(text(), 'Kinderwagens') or contains(text(), 'Kinderwagens')]
  3. //products/product[contains(text(), 'Kinderwagens') or contains(text(), 'Kinderwagens')]
  4. //products/product[contains(categorie, 'Kinderwagens') or contains(categorie, 'Kinderwagens')]

Note: | (Bitwise OR) operator don't be use

like image 23
Udhav Sarvaiya Avatar answered Sep 21 '22 09:09

Udhav Sarvaiya