How to Fetch Number of Rows and Columns from a Dynamic WebTable

Posted By : Neha Dahiya | 31-Jul-2018

Static and Dynamic HTML tables are published on the web.

  • Static tables: Type of table in which data is static i.e the number of rows and tables are fixed
  • Dynamic Tables: Type of table in which data is dynamic i.e number of rows and columns are not fixed. For example, on the basis of date filter number of rows get altered.

Using Selenium Web driver, the number of rows and columns can be predicted. The following can be found using this

  • Number of columns and rows in a web table
  • X row’s and Y column’s data

Static tables can be handled very easily but it is difficult to handle dynamic table as data in rows and columns is not constant.

 

Here is how you can fetch total number of rows and columns in a web table.

 

 

 

import java.text.ParseException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class Noofrowsandcols {
    public static void main(String[] args) throws ParseException {
    	WebDriver wd;
	  System.setProperty("webdriver.chrome.driver","G://chromedriver.exe");
	  wd= new ChromeDriver();
        wd.get("http://money.rediff.com/gainers/bsc/dailygroupa?");         
        //No.of Columns
        List  col = wd.findElements(By.xpath(".//*[@id=\"leftcontainer\"]/table/thead/tr/th"));
        System.out.println("No of cols are : " +col.size()); 
        //No.of rows 
        List  rows = wd.findElements(By.xpath(".//*[@id='leftcontainer']/table/tbody/tr/td[1]")); 
        System.out.println("No of rows are : " + rows.size());
        wd.close();
    }
}

Above code is explained as follows

Code Explanation:

  • Declare Web Driver object as “wd” and initialized chrome driver
  • Use List <WebElement> for total number of columns in “col”
  • Use findElement command to return list of all the elements which are matching to the specific locator
  • Using its X-path and findElement command get all the columns
  • Repeat the above steps to get the total number of rows.

 

Thanks!!

 

 

 

Related Tags

About Author

Author Image
Neha Dahiya

Neha is a bright QA Engineer with skills in manual testing . Apart from finding bugs in application, she loves sketching and painting.

Request for Proposal

Name is required

Comment is required

Sending message..