XPath匹配@class值和元素值? [英] XPath to match @class value and element value?
问题描述
我想找到这个元素,
<div class="item-price">$0.99</div>
使用XPath表示选择div
元素,这些元素的class
属性等于"item-price"
并且其内容包含美元符号($
).
using XPath which says select div
elements whose class
attribute equals "item-price"
and whose content contains a dollar sign ($
).
推荐答案
此XPath表达式:
//div[contains(@class, 'item-price') and contains(., '$')]
将匹配包含'$'的item-price
类的所有div
元素.
Will match all div
elements of the item-price
class containing a '$'.
如果要匹配@class
值中指定了多种CSS样式的情况,则在@class
的测试中使用contains()
很有用.
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.
注意:要获得更可靠的解决方案,请应用以下技术来避免意外的子字符串匹配(item-price
匹配,例如item-prices
):
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(., '$')]
这篇关于XPath匹配@class值和元素值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!