Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath to match @class value and element value?

Tags:

html

xml

xpath

I would like to locate this element,

<div class="item-price">$0.99</div>

using XPath which says select div elements whose class attribute equals "item-price" and whose content contains a dollar sign ($).

like image 313
Terrence Brannon Avatar asked Oct 21 '13 20:10

Terrence Brannon


People also ask

How can find XPath using two attributes?

The syntax for locating elements through XPath- Multiple Attribute can be written as: //<HTML tag>[@attribute_name1='attribute_value1'][@attribute_name2='attribute_value2]

Can you use XPath expression used to select the target node and its values?

XPath assertion uses XPath expression to select the target node and its values. It compares the result of an XPath expression to an expected value. XPath is an XML query language for selecting nodes from an XML. Step 1 − After clicking Add Assertion, select Assertion Category – Property Content.

What is the * indicates in XPath?

By adding '//*' in XPath you would be selecting all the element nodes from the entire document. In case of the Gmail Password fields, .//*[@id='Passwd'] would select all the element nodes descending from the current node for which @id-attribute-value is equal to 'Passwd'.

Can we use class attribute in XPath?

We can find an element using the attribute class name with Selenium webdriver using the locators - class name, css, or xpath. To identify the element with css, the expression should be tagname[class='value'] and the method to be used is By. cssSelector.


1 Answers

This XPath expression:

//div[contains(@class, 'item-price') and contains(., '$')]

Will match all div elements of the item-price class containing a '$'.

It's useful to use contains() in the test on @class if you want to match cases where there are multiple CSS styles specified in the @class value.


Caution: For a more robust solution, apply the following technique to avoid unintended substring matches (item-price matching, say, item-prices):

//div[contains(concat(' ',@class,' '), ' item-price ') and contains(., '$')]
like image 107
kjhughes Avatar answered Oct 18 '22 12:10

kjhughes