Quality Testing

Quality is delighting customers

How to count total number of rows of a table using Selenium?

Count total number of rows of a table using Selenium.

Views: 1321

Reply to This

Replies to This Discussion

Hi Madhavi Latha ,

Hope you are doing great.

Here is the link that you can follow for creating tables in selenium webdriver.


Software testing company

List {WebElement} total_rows = driver.findElements(By.className("//table[@id='tableID']/tr"));

int totalRow = total_rows.size();

Hope this will work.

Thanks Alisha and Sandhya !

Most of the software testing solutions encounter this issue as there several times in web applications that page returns the data in tabular forms using tables. Also, the tables itself can be static or dynamic as per the nature of the application.

Consider an example you search for a flight from Country A to Country B, it returns different no. of flights in tabular format on webpage depending on the availability, each time it will return the different numbers of flights.

You can use the below-written code to find out the no of rows & columns.

Code to find no. of rows:
WebElement getRows = driver.findElement(By.xpath("//table[@id='users_table']/tbody"));
List<WebElement>TotalRowsList = getRows.findElements(By.tagName("tr"));
System.out.println("Number of Rows in the table are : "+ TotalRowsList.size());

To find the no. of columns, first, you need to find out the no. of rows.

Code to find no. of columns:
WebElement GetColumns = driver.findElement(By.xpath("//table[@id='users_table']/tbody/tr"));
List<WebElement> TotalColsList = TGetColumns.findElements(By.tagName("td"));
System.out.println("Number of Columns in the table are: "+TotalColsList.size());


TTWT Magazine





© 2022   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service