I'm trying to learn XPath. I looked at the other contains()
examples around here, but nothing that uses an AND operator. I can't get this to work:
//ul[@class='featureList' and contains(li, 'Model')]
On:
...
<ul class="featureList">
<li><b>Type:</b> Clip Fan</li><li><b>Feature:</b> Air Moved: 65 ft.
Amps: 1.1
Clip: Grips any surface up to 1.63"
Plug: 3 prong grounded plug on heavy duty model
Usage: Garage, Workshop, Dorm, Work-out room, Deck, Office & more.</li><li><b>Speed Setting:</b> 2 speeds</li><li><b>Color:</b> Black</li><li><b>Power Consumption:</b> 62 W</li><li><b>Height:</b> 14.5"</li><li><b>Width:</b> Grill Diameter: 9.5"</li><li><b>Length:</b> 11.5"</li>
<li><b>Model #: </b>CR1-0081-06</li>
<li><b>Item #: </b>N82E16896817007</li>
<li><b>Return Policy: </b></li>
</ul>
...
The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]
Contains() is a method used in an XPath expression. It is used when the value of any attribute changes dynamically — for example, sign up information. The contain feature has an ability to find the element with partial text as shown in the below example.
text() and contains methods text(): A built-in method in Selenium WebDriver that is used with XPath locator to locate an element based on its exact text value. contains(): Similar to the text() method, contains() is another built-in method used to locate an element based on partial text match.
We will start to write XPath with //, followed by input tag, then we will use the select attribute, followed by its attribute name like name, id, etc, and then we will choose a value of attribute in single quotes. Here, (1 of 1) means exact match. It indicates that there is only one element available for this XPath.
You are only looking at the first li
child in the query you have instead of looking for any li
child element that may contain the text, 'Model'
. What you need is a query like the following:
//ul[@class='featureList' and ./li[contains(.,'Model')]]
This query will give you the elements that have a class
of featureList
with one or more li
children that contain the text, 'Model'
.
I already gave my +1 to Jeff Yates' solution.
Here is a quick explanation why your approach does not work. This:
//ul[@class='featureList' and contains(li, 'Model')]
encounters a limitation of the contains()
function (or any other string function in XPath, for that matter).
The first argument is supposed to be a string. If you feed it a node list (giving it "li
" does that), a conversion to string must take place. But this conversion is done for the first node in the list only.
In your case the first node in the list is <li><b>Type:</b> Clip Fan</li>
(converted to a string: "Type: Clip Fan
") which means that this:
//ul[@class='featureList' and contains(li, 'Type')]
would actually select a node!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With