0

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
  • List<WebElement> myTableRows = driver.findElements(By.cssSelector("#MyTableId tr")); Commented Apr 5, 2020 at 12:54

2 Answers 2

1

Try below solution :

List<WebElement> rowCount = driver.findElements(By.xpath("//tbody[@id='MyTableId']/tbody/tr"));
System.out.println("Num rows: " + rowCount .size());
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.