如何选择XPath元素数组中的前n个元素

时间:2016-08-04 19:17:10

标签: html xpath

我有一个包含多个单元格的网页,如示例中所示,我想先选择50个复选框...

<td class="col-xs-0">
<input type="checkbox" ng-click="selectedRun(record.file_name, record.run_status);" />
</td>

我尝试使用XPATH,如:

.//*/td[@class="col-xs-0"]//input[@ng-click="selectedRun(record.file_name, record.run_status);"][position() <= 50]

但它给了我页面上的所有节点,我需要先50 ...错误在哪里?感谢。

1 个答案:

答案 0 :(得分:0)

position()给出每个iterable相对于其父项的索引,从xpath获取的input元素相对于td而且没有50 {{1}每个input下面的元素。

相反,以下xpath适用于您:

td

此xpath首先获取符合条件的所有//td[@class="col-xs-0" and count(child::*[name()="input"][@type="checkbox" and @ng-click="selectedRun(record.file_name, record.run_status);"])>0][position()<=50],然后查找位置。

示例输入:

td

xpath <root> <td class="col-xs-0"> <input type="checkbox" ng-click="selectedRun(record.file_name, record.run_status);" /> </td> <td class="col-xs-0"> <input type="checkbox" ng-click="selectedRun(record.file_name, record.run_status);" /> </td> <td class="col-xs-0"> <input type="checkbox" ng-click="selectedRun(record.file_name, record.run_status);" /> </td> <td class="col-xs-0"> <input type="checkbox" ng-click="selectedRun(record.file_name, record.run_status);" /> </td> <td class="col-xs-0"> <input type="checkbox" ng-click="selectedRun(record.file_name, record.run_status);" /> </td> <td class="col-xs-0"> <input type="checkbox" ng-click="selectedRun(record.file_name, record.run_status);" /> </td> <td class="col-xs-0"> <input type="checkbox" ng-click="selectedRun(record.file_name, record.run_status);" /> </td> <td class="col-xs-0"> <input type="radio" ng-click="selectedRun(record.file_name, record.run_status);" /> </td> <td class="col-xs-0"> <input type="radio" ng-click="selectedRun(record.file_name, record.run_status);" /> </td> <td class="col-xs-0"> <input type="radio" ng-click="selectedRun(record.file_name, record.run_status);" /> </td> <td class="col-xs-0"> <input type="radio" ng-click="selectedRun(record.file_name, record.run_status);" /> </td> </root> 返回:

//td[@class="col-xs-0" and count(child::*[name()="input"][@type="checkbox" and @ng-click="selectedRun(record.file_name, record.run_status);"])>0][position()<=3]

xpath Element='<td class="col-xs-0"> <input type="checkbox" ng-click="selectedRun(record.file_name, record.run_status);" /> </td>' Element='<td class="col-xs-0"> <input type="checkbox" ng-click="selectedRun(record.file_name, record.run_status);" /> </td>' Element='<td class="col-xs-0"> <input type="checkbox" ng-click="selectedRun(record.file_name, record.run_status);" /> </td>' 返回的位置:

//td[@class="col-xs-0" and count(child::*[name()="input"][@type="radio" and @ng-click="selectedRun(record.file_name, record.run_status);"])>0][position()<=2]