当我使用下面的代码片段时,我想获取给定行中的列数
int size = element.findElement(By.xpath("/tr[" + Row + "]")).findElements(By.tagName("td")).size();
我收到一个异常,指出 NoSuchElementException。
下面是 HTML block
<table class="DynamicOrderTable" id="customerOrderHeader">
<tbody>
<tr style="background-color: rgb(246, 246, 246);">
<th>Order Number</th>
<th>Version</th>
<th>Customer Name</th>
<th>Order Status</th>
<th>Order Sub-status</th>
</tr>
<tr class="Temp">
<td class="close" id="orderNumber">
<div class="clickableText">1234</div>
</td>
<td class="close">1</td>
<td class="close" id="customerName">ABC101</td>
<td title="OrderResponse Sequence Number:14" class="close" id="orderStatus"><div class="clickableText">Complete</div></td>
<td class="close">Closed</td>
</tr>
</tbody>
</table>
当我们有 thead 和 tbody 标签时,如何获取一行中的列数?
最佳答案
您获取列大小的代码是正确的,只需在您的 xpath
中将 /tr
更改为 .//tr
为:-
WebElement element = driver.findElement(By.id("customerOrderHeader"));
int size = element.findElement(By.xpath(".//tr[" + Row + "]")).findElements(By.tagName("td")).size();
或者
int size = element.findElements(By.tagName("tr")).get(Row).findElements(By.tagName("td")).size();
关于java - 如何获取 <tr> 行中 <td> 或 <th> 的列数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39383213/