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

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

问题描述

我想定位这个元素,

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

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