Reputation: 1788
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>
Upvotes: 0
Views: 1716
Reputation: 4177
Try below solution :
List<WebElement> rowCount = driver.findElements(By.xpath("//tbody[@id='MyTableId']/tbody/tr"));
System.out.println("Num rows: " + rowCount .size());
Upvotes: 1
Reputation: 577
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);
Upvotes: 0