我有这个链接:
<a href="/stores/non-consequatur-totam/products/search?term=yellow">
Search all categories in
<span style="font-weight: bold;">non consequatur totam's</span>
store for “yellow”
</a>
我试图使用xpath匹配它,但我只能匹配span之前的部分,使用contains和text()。我正在使用水豚。
page.all(:xpath, "//a[contains(text(), 'Search all categories in')]").first
=> #<Capybara::Element tag="a" path="/html/body/div[2]/div[3]/div[2]/div[1]/p[2]/a[1]">
page.all(:xpath, "//a[contains(text(), 'store for')]").first
=> nil
我如何匹配实际文本,就像jQuery一样?我需要忽略内部的html标签。我真的希望能够匹配跨度中的部分。
答案 0 :(得分:2)
我认为你要找的是一个元素的字符串值。元素节点的字符串值只是其所有后代文本节点的串联。
几乎使用表达式string(//a)
(以避免文本内容中的单引号或双引号存在任何不相关的问题)您显示的HTML代码段:
<a href="/stores/non-consequatur-totam/products/search?term=yellow">
Search all categories in
<span style="font-weight: bold;">non consequatur totams</span>
store for yellow
</a>
产量
[EMPTY OUTPUT LINE]
Search all categories in
non consequatur totams
store for yellow
[EMPTY OUTPUT LINE]
以及a
元素中的所有文字。现在,要将a
元素与其文本内容进行匹配,请在谓词中测试string()
:
//a[normalize-space(string(.)) = 'Search all categories in non consequatur totams store for yellow']
再次应用于略微修改的输入,这将返回链接元素。
normalize-space()
是必要的,因为文本内容包含换行符,而且很难包含在XPath表达式中。
回复你的评论并举一个例子:
我真的希望能够使用单个包含,假设我有
<a>a b <span>c</span d</div>
我想查找a b c
我认为你的意思是搜索
<a>a b <span>c</span> d</a>
并寻找a b c d
?使用上述方法,使用
//a[normalize-space(string(.)) = 'a b c d']