How to access web table element in selenium webdriver

How to access web table element in selenium web driver

There are 2 types of HTML table possible in the web.
  • Static web table.
  • Dynamic web table. 
Static web table:  Normally static tables are static in nature, means a number of column and row is fixed, it will never increase in runtime and data inside the each cell is also fixed.
Dynamic web table:  It is just opposite of static web table, here row, column and cell data could be not fixed or we can say dynamic.  It will change at runtime. 

We need to follow few steps to retrieve data from web table 

  1. We need to uniquely identify table object.
  2. Count number of rows present in the table.
  3. Count number of column present in each row. 
  4. Get the value from each cell.  

Step 1. We need to uniquely identify table object

Now we can start with the below table. We need to identify the web table uniquely. 

how to find element from web table using selenium

how to find element from web table using selenium
Here we can see there an id for that highlighted table.  The id is “ customers “.  We know how to identify the web element using XPath or other selenium locators, you can refer my previous post also. 

XPath for this table object


Java code 


Step 2: Count Number of rows present in the table.

In HTML table row denoted as <tr>. So we need to count the number of <tr> present under the table. 
We can directly count the number of rows is available on the table. 

how to find element from web table using selenium

XPath for row count

Above XPath will return 7 element

Java code 

int rowCount = driver.findElements(By.xpath("//*[@id='customers']/child::*/child::tr")).size()

Step 3: Count Number of column present in each row.

Now we need to iterate each row and find out how many columns are there for that row. 

how to find web element from web table using selenium

Xpath for column count for first row (except header)
Above XPath will return 3 <td> tag

Java code  

int columnCount = driver.findElements(By.xpath//[@id='customers']/child::*/child::tr[1]/child::td")).size()

Step 4: Get the value from each cell.

Java code


Complete java code to iterate each row and column and print the value 

 public static void main(String argc[]){  
 driver = new ChromeDriver() ;  
 int rowCount = driver.findElements(By.xpath("//*[@id='customers']/child::*/child::tr")).size();  
 for (int i=1;i<=rowCount;i++){  
   int columnCount =  driver.findElements(By.xpath("//*[@id='customers']/child::*/child::tr["+i+"]/child::td")).size();  
    for(int j=1;j<=columnCount;j++){  
      WebElement element =  driver.findElement(By.xpath("//*[@id='customers']/child::*/child::tr["+i+"]/child::td["+j+"]"));  
      System.out.println(element.getText()+ "  ");  

Share this

Related Posts

Next Post »


November 21, 2017 at 9:39 PM delete

Great put Good stuff.All the topics were explained quickly understand for me.I am waiting for your next fantastic blog.Thanks for sharing.Any coures related details learn...

selenium training in bangalore|

May 3, 2018 at 10:00 PM delete

Thanks for the useful information of software testing courses, give more updates on software testing development, First time I visit your blog really nice, I bookmark your blog here after a daily visit. Learn software testing course in Chennai

July 20, 2018 at 2:37 AM delete

Thanks for your informative post. Selenium is the future of IT world to improve the quality of products.

Selenium Training institute in Chennai
Selenium Course in Chennai

December 14, 2019 at 9:36 AM delete

Thank you so much for the great and very beneficial stuff that you have shared with the world.

Become an Expert In Software Testing Training! The most trusted and trending Programming Language. Learn from experienced Trainers and get the knowledge to crack a coding interview, @Softgen Infotech Located in BTM Layout.

March 25, 2022 at 5:58 AM delete

I'm truly grateful for this article. This is exactly what I've been looking for. Turkey visa for Mexico is a visa for Mexican people. With this visa all the Mexicans visit Turkey .


your ad