XPath Axes 示例

Example 1:Use Button 3 to climb up to its TABLE

This is similar to example we saw in the section 'Select Parent Nodes using Children' but instead of using the XPath expression /..,we will use axes.The target HTML and XPath axis is shown below:

<table id='table1'> "!-- destination -->

  <tr class='row1' id=BPT>

    <td>View ID</td>

    <td><input type='button' value='Button 1' class='btn_blue' id='btnfirst'></td>

  </tr>

  <tr class='row2' id=QC>

    <td>View Address</td>

    <td><input type='button' value='Button 2' class='btn_blue'></td>

  </tr>

  <tr class='row3' id=QTP>

    <td>View Phone Number</td>

    <td><input type='button' value='Button 3' class='btn_green'></td>

  </tr>

</table>

Following are the 2 possible XPath axes used to move up from the button (<input>) to its parent Table:

1. //input[@value='Button 3']/parent::td/parent::tr/parent::tbody/parent::table

2. (//input[@value='Button 3'])[1]/ancestor::table

 

In QTP:

'Use Button 3 to climb up to its TABLE

Browser("XPath").WebElement("xpath:=(//input[@value='Button 3'])[1]/ancestor::table").Click

 

Example 2:Use TR[@class=row4] row to identify preceding rows

This example shows how one anchor row can be used to identify neighboring rows.

<tr class='row1' id=BPT>  <!-- destination -->

  <td>View ID</td>

  <td><input type='button' value='Button 4' class='btn_blue'></td>

</tr>

<tr class='row2' id=QC>  <!-- destination -->

  <td>View Address</td>

  <td><input type='button' value='Button 5' class='btn_blue'></td>

</tr>

<tr class='row3' id=QTP>  <!-- destination -->

  <td>View Phone Number</td>

  <td><input type='button' value='Button 6' class='btn_green'></td>

</tr>

<tr class='row4' id=QC>  <!-- destination -->

  <td>View Fax Number</td>

  <td><input type='button' value='Button 7' class='btn_green'></td>

</tr>

1. Indentify the first row with attribute ROW1: (//tr[@class='row4']/preceding-sibling::tr)[1]

2. Indentify the first row with attribute ROW2: (//tr[@class='row4']/preceding-sibling::tr)[2]

3. Indentify the first row with attribute ROW3: (//tr[@class='row4']/preceding-sibling::tr)[3]

 

In QTP:

'1. Indentify the first row with attribute ROW1

Browser("XPath").WebElement("xpath:=(//tr[@class='row4']/preceding-sibling::tr)[1]").Click

 

Example 3:Use View ID to Indentify Button 1

This example is a little different from the previous ones we have seen.Here we show how to use View ID(TD node) to move to the sibling TD node and drill down once to select the CHILD node:

<td>View ID</td> <!-- target -->

<td>

  <input type='button' value='Button 1' class='btn_blue' id='btnfirst'> <!-- destination -->

</td>

1. (//td[.='View ID'])[1]/following-sibling::td/child::input

 

In QTP:

Browser("XPath").WebButton("xpath:=(//td[.='View ID'])[1]/following-sibling::td/child::input").Click

 

Example 4:Use View ID to indentify Button 4

<td>View ID</td> <!-- target -->

<td>

  <input type='button' value='Button 4' class='btn_blue'> <!-- destination -->

</td>

1. (//td[.='View ID'])[2]/following-sibling::td/child::input

 

In QTP:

Browser("XPath").WebElement("xpath:=(//td[.='View ID'])[2]/following-sibling::td/child::input").Click

 

Example 5:Use View Address row to indentify its TD cells

<tr class='row2' id=QC> <!-- target -->

  <td>View Address</td> <!-- destination -->

  <td> <!-- destination -->

    <input type='button' value='Button 2' class='btn_blue'>

  </td>

</tr>

1. First Cell: (//tr[@class='row2'])[1]/descendant::td[1]

2. Second Cell: (//tr[@class='row2'])[1]/descendant::td[2]

 

In QTP:

'First cell

Browser("XPath").WebElement("xpath:=(//tr[@class='row2'])[1]/descendant::td[1]").Click

posted @ 2013-11-21 12:29  dushuai  阅读(340)  评论(0编辑  收藏  举报