2014-05-18 2 views
0

xpath로 구문 분석하려고하는 다음 웹 페이지가 있습니다.Xpath로 HTML 구문 분석하기

첫 번째 테이블의 경우 xpath //*[@id="eForm"]/table[1]이 잘 작동하고 두 번째 테이블의 경우 xpath //*[@id="eForm"]/table[2]이 정상적으로 작동합니다.

그러나 페이지 순서가 변경 될 경우 내 성명서에 친척을 사용하지 마십시오.

두 번째 테이블의 경우 에이전트의 스팬 id 다음에옵니다. 내가 xpath에 스팬 ID 다음의 테이블을 지정할 수 있는지 누가 알 수 있습니까?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html lang="en"> 
<head> 
    <title>Blah</title> 
    <style type="text/css"> 

    </style> 


</head> 
<body class="frame"> 
    <div id="container"> 
     <div id="maincontent"> 
      <h1>Details</h1> 
      <br> 
      <form name="Form" method="post" action="xxx" id="eForm"> 
       <input type="hidden" name="__VIEWSTATE" value="xxx" /> 


       <table class="summary"> 
        <thead> 
         <tr> 
          <th colspan="2"> 
           SUmmary Heading 
          </th> 
         </tr> 
        </thead> 

        <tr> 
         <th width="40%"> 

         </th> 
         <td> 
          ABC Ltd 
         </td> 
        </tr> 
        <tr> 
         <th> 
          Number 
         </th> 
         <td> 
          1234 
         </td> 
        </tr> 
        <tr> 
         <th> 
          Company Type 
         </th> 
         <td> 
          Normal Type 
         </td> 
        </tr> 
        <tr> 
         <th> 
          Office 
         </th> 
         <td> 
          Street Address is Here 
         </td> 
        </tr> 
        <tr> 
         <th> 
          Date of Incorporation 
         </th> 
         <td> 
          24/02/2014 
         </td> 
        </tr> 




        <tr> 
         <th> 
          Status 
         </th> 
         <td> 
          LIVE 
         </td> 
        </tr> 

       </table> 
       <span id="Agent"> 
        <br> 
       </span> 
       <table class="summary"> 
        <thead> 
         <tr> 
          <th colspan="2"> 
           Agent Details 
          </th> 
         </tr> 
        </thead> 

        <tr> 
         <th width="40%"> 
          Registered Agent 
         </th> 
         <td>ABC COMPANY LIMITED</td> 
        </tr> 
        <tr> 
         <th> 
          Agent Address 
         </th> 
         <td>Street Address</td> 
        </tr> 

       </table> 


       <!----><br> 
       <br> 
      </form> 
     </div> 
    </div> 
</body> 
</html> 

답변

0

답변 하시겠습니까?

//form[@id='eForm']//span[@id='Agent']//table[@class='summary'] 

은 또한 당신이 1 일 테이블 식별이 XPath를 사용할 수 있습니다 : 당신은 당신 같은 XPath를 할 수있는 것을해야 할 일

//form[@id='eForm']//table//thead//th[text()='SUmmary Heading'] 

그리고 이것은 2 차 테이블 식별 :

//form[@id='eForm']//table//thead//th[text()='Agent Details'] 

이 경우 친척을 사용할 필요가 없습니다.

+0

감사합니다,하지만 난이 내 응용 프로그램에서 동작하지 않습니다 - 거기에 어떤 도구를 알고 웹 페이지에 대한 구문을 확인하기 위해? – John

+0

@ 존, FireFoxe의 Firebug + Firepath 플러그인을 사용할 수 있습니다. 나는 초기 응답을 편집했다. // // [@ id = 'eForm']''// form [@ id = 'eForm']'또는'// * [@ id = 'eForm' ]' –

+0

@Alexander, 제 대답을 고쳐 주셔서 감사합니다. John Alexander는 Firebug를 사용하여 xpathes를 확인할 수 있다고 말했습니다. 그래도 문제가 있다면 알려주세요. – Andrii

0

XPath following-sibling을 사용하여 다른 요소 뒤에 오는 요소를 지정할 수 있습니다. 사례에 대한 사례 :

//form[@id='eForm']/span[@id='Agent']/following-sibling::table 

또는 다른 방법을 시도해 볼 수 있습니다. 선택 표는 <span id="Agent"> 요소 앞의 한 : 답장을 보내

//form[@id='eForm']/table[preceding-sibling::span[@id='Agent']]