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: 472

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.

https://stackoverflow.com/questions/33233883/getting-the-count-of-r...

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

RSS

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2020   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service