Header Ads

How To Get Table Rows in Selenium??

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

Below is an example of a table.

Name Salary
Animesh 5000
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();
Powered by Blogger.