如何在xpath中选择多个标记

时间:2016-03-31 10:00:25

标签: java selenium xpath html-table

请访问网站“http://www.cricbuzz.com/cricket-series/2223/icc-cricket-world-cup-2015/points-table

<table class="table cb-srs-pnts">
<thead>
<tr class="cb-srs-gray-strip">
<th class="cb-col-20 cb-srs-pnts-th text-left" style="padding-left: 6px;">Pool B</th>
<td class="cb-srs-pnts-th">Mat</td>
<td class="cb-srs-pnts-th">Won</td>
<td class="cb-srs-pnts-th">Lost</td>
<td class="cb-srs-pnts-th">Tied</td>
<td class="cb-srs-pnts-th">NR</td>
<th class=" cb-srs-pnts-th">Pts</th>
<td class="cb-srs-pnts-th">NRR</td>
<th/>
</tr>
</thead>
<tbody>
</table>

我必须打印第一张桌子的所有表格标题。它有“td”和“th”标签的组合。

我使用以下xpath来检索它们。

//h3/../table[1]/thead/tr/*[self::td or self::th]

除了文本“Pool B”之外,所有值都被打印 有人可以告诉我为什么“Pool B”文本没有被选中?

打印输出的代码:

driver.get("cricbuzz.com/cricket-series/2223/icc-cricket-world-cup-2015/…); 
System.out.println(driver.findElement(By.xpath(" //h3/../table[1]/thead/tr/th")‌​).getText()); 
List<WebElement> tableHeading = driver.findElements(By .xpath("//h3/../table[1]/thead/tr/*[self: : tdorself: : th]")); 
for (int i = 1; i < tableHeading.size(); i++) 
{ 
   System.out.println(i+""+tableHeading.get(i).getText()); 
}

1 个答案:

答案 0 :(得分:1)

在for循环中将索引设置为0:

for (int i = 0; i < tableHeading.size(); i++) 
{ 
   System.out.println(i+""+tableHeading.get(i).getText()); 
}