How To Get Table Rows in Selenium??

Share and Enjoy !

This is a very important implementation for selenium to find rows of a given table.

Below is an example of a table.

NameSalary
Animesh5000
Rohan 7000

Now if we want to get rows of the table,we need to perform the following steps:

  1. First go Inspect element or firebug to determine the table name or id.[we can get the table by tag name also]
2.Now the corresponding code goes like
int count = 0;
List rows = driver.findElements(By.xpath("//table//tr"));
for(WebElement row: rows){
if(row.isDisplayed())
count++;
}
System.out.println("The number of rows that are visible is: "+ count);

instead xpath we can also find it by id or name like-

WebElement table=driver.findElement(By.id("Employee"));
if name is there
WebElement table=driver.findElement(By.name("name of the table"));
List rows=table.findElement(By.tagName("tr"))
rows.size();

Share and Enjoy !

READ  How to Resolve Issue With Size() Function in Selenium for ChromeDriver

One thought on “How To Get Table Rows in Selenium??

  1. Hi there this is somewhat of off topic but I was wanting to know ifblogs use WYSIWYG editors or if you have to manually code withHTML. I’m starting a blog soon but have no coding expertise so I wanted to get guidance from someone with experience.Any help would be greatly appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *