繁体   English   中英

如何在 xpath 中选择多个标签

[英]How to select multiple tags in an xpath

请访问网站“ 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()); 
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM