ChatGPT解决这个技术问题 Extra ChatGPT

如何编写 XPath 查询来匹配两个属性?

以下问题:

<div id="id-74385" class="guest clearfix" style="z-index: 999;">

综上所述,

如果我想要一个同时检查 id 和 class 的 XPath 表达式,我们可以用“和”条件来做吗:

//div[@id='id-74385'] and div[@class='guest clearfix']

这是正确的方法吗?我的执行在这里失败......请帮助!


B
Brian Agnew
//div[@id='..' and @class='...]

应该做的伎俩。即选择具有所需值的 both 属性的 div 运算符。

值得使用其中一个 online XPath testbeds 来尝试一下。


许多在线工具都要求符合 XML。对于 HTML,使用浏览器的 XPath 实现更容易进行测试。这是一个 code sample,以及一个将迭代器转换为数组的辅助函数。
z
zhangyangyu

//div[@id='id-74385'][@class='guest clearfix']


C
CodeMonkey

添加到布赖恩阿格纽的答案。

您也可以使用 //div[@id='..' or @class='...],并且您可以在 //div[@id='..' and (@class='a' or @class='b')] 中使用带括号的表达式。


M
Manjesh

示例 XML:

<X>
<Y ATTRIB1=attrib1_value ATTRIB2=attrib2_value/>
</X>

string xPath="/" + X + "/" + Y +
"[@" + ATTRIB1 + "='" + attrib1_value + "']" +
"[@" + ATTRIB2 + "='" + attrib2_value + "']"

XPath 测试平台:http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm