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