【问题标题】:Xpath: Select parent link matching three child attributesXpath:选择匹配三个子属性的父链接
【发布时间】:2013-04-01 18:51:43
【问题描述】:

我需要使用 xpath 选择符合以下三个条件的链接:

父 @class= 'testItem'

child @class= 'icon icon_checked'

text = '测试文本放在这里!'

我不确定将 text 属性放在 xpath 引用中的什么位置。我已经尝试了以下许多排列:

//a[@class="testItem" and child::span[@class="icon icon_checked"] and li[text()="test text goes here!"]]

我的问题是文本部分不在它自己的范围内。

这是原始示例:

<li>
<a class="testItem2" data-code="2" href="javascript:void(0);">
<span class="icon icon_checked"></span>
test text goes here2!
</a>
</li>

<li>
<a class="testItem" data-code="2" href="javascript:void(0);">
<span class="icon icon_checked"></span>
test text goes here!
</a>
</li>

【问题讨论】:

  • 多种解决方案,您可能遇到的一些问题可能源于文本实际上以换行符开头并以换行符结尾。您可能想使用contains()normalize-space()

标签: xpath


【解决方案1】:

感谢您的帮助。我找到了答案。

我可以简单地将 xpath 的最后一部分从 li[text()="test text goes here!"] 更改为 .[text()="test text goes here!"]]。

我的最终工作 xpath 是:

//a[@class='testItem' and child::span[@class='icon icon_checked'] and .[text()='test text goes here!']]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多