XPath /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
有效,但我遇到的问题是每行有两个//img[contains(@class, 'check')]/ancestor::tr//span[contains(@class, 'date-display-single')]
。
结果是:
<span>
我想要的是第一个[nodeValue] => Friday 7 October 2016
[nodeValue] => Fri 7 Oct 2016
[nodeValue] => Friday 21 October 2016
[nodeValue] => Fri 21 Oct 2016
[nodeValue] => Friday 4 November 2016
[nodeValue] => Fri 4 Nov 2016
:
<span>
我该怎么做?
修改:HTML
[nodeValue] => Friday 7 October 2016
[nodeValue] => Friday 21 October 2016
[nodeValue] => Friday 4 November 2016
答案 0 :(得分:1)
此XPath,基于您的原始XPath,
//img[contains(@class, 'check')]/ancestor::tr/td[1]/span[contains(@class, 'date-display-single')]
将仅选择每个陈述span
中第一个td
中所述的tr
元素:
<span class="date-display-single">Friday 7
October 2016</span>
<span class="date-display-single">Friday 14
October 2016</span>
按要求。
但是,请注意,这个较短的XPath也会选择相同的元素:
//tr[.//img[@class='check']]/td[1]/span[@class='date-display-single']
只要您可以假设@class
属性的单值,这就没问题了。如果你不能,你的解决方案有一个严重的子串问题,其标准解决方案涉及连接目标类值周围的空格:
[contains(concat(' ',@class,' '), ' check ')]
此技术可应用于check
和date-display-single
测试的上述XPath。
答案 1 :(得分:0)
您可以使用索引,例如:
(your XPath here)[1]