2016-10-06 4 views
0

XPath //img[contains(@class, 'check')]/ancestor::tr//span[contains(@class, 'date-display-single')]이 작동하지만, 문제는 각 행에 해당 클래스가 두 개 있습니다 (<span>). 내가 원하는 것은 처음 <span>입니다XPath : 특정 속성이있는 첫 번째 요소를 선택하십시오.

[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 

: 결과

[nodeValue] => Friday 7 October 2016 
[nodeValue] => Friday 21 October 2016 
[nodeValue] => Friday 4 November 2016 

내가 어떻게 할 수 있습니까?

편집 : HTML은

<table class="views-table cols-6"> 
    <thead> 
     <tr> 
      <th class="a" scope="col"></th> 
      <th class="a" scope="col"></th> 
      <th class="a" scope="col">1</th> 
      <th class="a" scope="col">2</th> 
      <th class="a" scope="col">3</th> 
      <th class="a" scope="col">4</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr class="odd views-row-first"> 
      <td class="a"><span class="date-display-single">Friday 7 
      October 2016</span></td> 
      <td class="a"><span class="date-display-single">Fri 7 Oct 
      2016</span></td> 
      <td class="a"><img alt="Yes" class="check" src="image.png" 
      title="Yes"></td> 
      <td class="a"></td> 
      <td class="a"></td> 
      <td class="a"></td> 
     </tr> 
     <tr class="even"> 
      <td class="a"><span class="date-display-single">Friday 14 
      October 2016</span></td> 
      <td class="a"><span class="date-display-single">Fri 14 Oct 
      2016</span></td> 
      <td class="a"></td> 
      <td class="a"><img alt="Yes" class="check" src="image.png" 
      title="Yes"></td> 
      <td class="a"><img alt="Yes" class="check" src="image.png" 
      title="Yes"></td> 
      <td class="a"><img alt="Yes" class="check" src="image.png" 
      title="Yes"></td> 
     </tr> 
    </tbody> 
</table> 
그것은 귀하의 게시물과 같은

은 대부분 코드; 자세한 내용을 추가하십시오.

+0

XPath에 문제가있는 HTML 축소 샘플을 게시하십시오. 감사. – kjhughes

+0

Added.123456789 –

답변

0

당신은 인덱스를 사용할 수 있습니다, 예를 들면 :

(your XPath here)[1] 
+0

'(// img [contains (@class, 'check')]/ancestor :: tr // span [포함 (@class, '날짜 표시 단일]))) [1]' 는 나를 준다 :'[nodeValue] => 2016 년 10 월 7 일 금요일'내가 원하는 것이 아니다. –

+0

@FlutterShy 이것은 다음을 시도한다 :'// img [포함 (@class, 'check')]/ancestor :: tr [1]' –

+0

은'[1]'없이 정확히 같은 것을 준다. –

1

원래 XPath를 기반으로이 XPath를,

//img[contains(@class, 'check')]/ancestor::tr/td[1]/span[contains(@class, 'date-display-single')] 

가 내 첫 td에있는 경우에만 명시된 span 요소를 선택합니다 각각은 tr :

<span class="date-display-single">Friday 7 
      October 2016</span> 
<span class="date-display-single">Friday 14 
      October 2016</span> 

요청시.

참고 그러나이 짧은 XPath는 같은 요소를 선택합니다 : 이것은만큼 당신이 @class 속성에 대한 단일 값을 가질 수 괜찮

//tr[.//img[@class='check']]/td[1]/span[@class='date-display-single'] 

. 당신이하지, 솔루션 누구의 표준 용액 대상 클래스 값에 공백을 연결 관련된 심각한 문자열 문제가있는 경우 :

[contains(concat(' ',@class,' '), ' check ')] 

이 기술은 모두 checkdate-display-single 시험에 대한 위의 XPath에 적용 할 수있다.

+0

나는'td [n]'을 사용할 수 있다는 것을 알고있다. ,하지만 레이아웃을 약간 변경하면 작업을 쉽게 중단 할 수 있으므로 그렇게하지 않는 것이 좋습니다. –

+1

그런 다음 원하는 위치에서 원하지 않는 것을 모호하게 만드는 위치 기반 조건을 말해주십시오. 그러면 XPath를 보여 드리겠습니다. – kjhughes

관련 문제