如何匹配包含特定字符串的属性? [英] How can I match on an attribute that contains a certain string?

查看:31
本文介绍了如何匹配包含特定字符串的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当属性包含多个单词时,我在按属性选择节点时遇到问题.例如:

这是我的 xpath 表达式:

//*[@class='atag']

该表达式适用于

但不适用于前面的示例.如何选择

?

解决方案

这是一个查找 className 包含 atag 的 div 元素的示例:

//div[contains(@class, 'atag')]

下面是一个查找 className 包含 atagbtag 的 div 元素的示例:

//div[contains(@class, 'atag') and contains(@class ,'btag')]

但是,它也会找到部分匹配项,例如 class="catag bobtag".

如果您不想部分匹配,请参阅下面 bobince 的回答.

I am having a problem selecting nodes by attribute when the attributes contains more than one word. For example:

<div class="atag btag" />

This is my xpath expression:

//*[@class='atag']

The expression works with

<div class="atag" />

but not for the previous example. How can I select the <div>?

解决方案

Here's an example that finds div elements whose className contains atag:

//div[contains(@class, 'atag')]

Here's an example that finds div elements whose className contains atag and btag:

//div[contains(@class, 'atag') and contains(@class ,'btag')]

However, it will also find partial matches like class="catag bobtag".

If you don't want partial matches, see bobince's answer below.

这篇关于如何匹配包含特定字符串的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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