Tal Angel
Tal Angel

Reputation: 1788

How to count the number of rows in a table using Selenium Java

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

Answers (2)

SeleniumUser
SeleniumUser

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

manikanta nvsr
manikanta nvsr

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

Related Questions