XPath匹配@class值和元素值? [英] XPath to match @class value and element value?

查看:546
本文介绍了XPath匹配@class值和元素值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到这个元素,

<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆