2014-05-18 5 views
0

J'ai la page web suivante que j'essaye d'analyser avec xpath.Analyse HTML avec Xpath

Pour la première table, le xpath //*[@id="eForm"]/table[1] fonctionne correctement et pour la deuxième table le xpath //*[@id="eForm"]/table[2] fonctionne correctement.

Cependant ce que je voudrais faire n'est pas utiliser des parents dans ma déclaration au cas où l'ordre de page change.

Pour la deuxième table, il s'agit d'un ID d'étendue de l'agent. Quelqu'un peut-il me dire si je peux spécifier dans xpath la table suivant un ID de span?

<!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> 

Répondre

0

Répondez à votre question - oui. Pour ce faire, vous pouvez vous comme XPath:

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

vous pouvez également utiliser XPath pour identifier le tableau 1-er:

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

Et cela pour identifier le tableau 2-ème:

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

Dans ce cas, vous n'avez pas besoin d'utiliser des parents.

+0

Merci pour votre réponse, mais je ne peux pas obtenir ce travail dans ma demande - Y at-il des outils pour aider à vérifier la syntaxe contre un savoir page Web? – John

+0

@John, vous pouvez utiliser les plugins Firebox Firepath de FireFoxe. BTW, j'ai édité la réponse initiale, '// [@ id = 'eForm']' devait être '// form [@ id = 'eForm']' ou '// * [@ id = 'eForm' ] ' –

+0

@Alexander, merci d'avoir corrigé ma réponse. John as Alexander a dit que vous pouvez vérifier vos xpathes en utilisant Firebug. S'il vous plaît laissez-moi savoir si vous avez encore des problèmes avec cela. – Andrii

0

Vous pouvez utiliser XPath following-sibling pour spécifier un élément suivant un autre élément. Exemple pour votre cas:

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

Ou vous pouvez essayer d'une autre façon; table de sélection ayant <span id="Agent"> élément précédent:

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