英文:
How to count the number of rows in a table using Selenium Java
问题
我在HTML网页中有一个包含X行的表格。如何使用Selenium Java以及CSS或XPath表达式来计算行数?
我的表格示例:
<tbody id="MyTableId">
<tr>第一行数据</tr>
<tr>第二行数据</tr>
<tr>第三行数据</tr>
</tbody>
代码部分不要翻译。
英文:
I have a table with X rows in an HTML web page. How do I count the number of rows using Selenium Java and CSS or XPath expressions?
Example of my table:
<tbody id="MyTableId">
<tr> First Data row</tr>
<tr> 2nd Data row</tr>
<tr> 3rd Data row<tr>
</tbody>
答案1
得分: 1
尝试以下解决方案:
List<WebElement> rowCount = driver.findElements(By.xpath("//tbody[@id='MyTableId']/tbody/tr"));
System.out.println("Num rows: " + rowCount.size());
英文:
Try below solution :
List<WebElement> rowCount = driver.findElements(By.xpath("//tbody[@id='MyTableId']/tbody/tr"));
System.out.println("Num rows: " + rowCount .size());
答案2
得分: 0
你也可以使用以下代码行而不是使用 xpath:
WebElement table = driver.findElement(By.id("MyTableId"));
int numberOfRows = table.findElements(By.tagName("tr")).size();
System.out.println("table row count : " + numberOfRows);
英文:
You can also use the below lines without xpath :
WebElement table = driver.findElement(By.id("MyTableId"));
int numberOfRows = table.findElements(By.tagName("tr")).size();
System.out.println("table row count : "+numberOfRows);
专注分享java语言的经验与见解,让所有开发者获益!
评论