Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Handle Dynamic Web Tables using Selenium WebDriver in Java?

Improve Article
Save Article
Like Article
  • Last Updated : 27 Apr, 2022
Improve Article
Save Article
Like Article

A Dynamic web table is a type of web table where the number of rows and columns are not constant, they keep changing from time to time depending on the requirements or based on the query, i.e. Number of rows and columns is NOT fixed.

Example of HTML Table

Dynamic Table

The below table is a dynamic web table and the HTML code for the table, This table does not contain even an arrangement of rows and columns like the last row has two columns only, but the other has 4 columns. 


      <table name="Table">
            <td>Learn Selenium</td>
            <td>Learn Java</td>
            <td>Learn JS</td>
            <td>Master In Selenium</td>
            <td>Master In Java</td>
            <td>Master In JS</td>

Save the code as “.html”, then you will get an HTML table like below.


Example for fetch no. of rows and columns from Dynamic Table

The main problem that occurs during working with the Dynamic table is, that we cannot predict the number of rows and columns. So in this example, we will use the Selenium web driver to find the number of rows and columns. For computing the number of rows and columns, we require the Xpath of the web table.

Find the X-Path of the Table:

Go to the website, Right-click on the table and select inspect and copy the x-path.

X-Path for Columns:


X-Path for Rows:




import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.testng.annotations.Test;
public class Geeks {
    public void geeksforgeeks(){
        ChromeDriver driver = new ChromeDriver();
        // Maximize the browser
        // Launch Website
        // Number of columns
        List <WebElement> col = driver.findElements(By.xpath("/html/body/table/tbody/tr[1]/th"));
        System.out.println("No of columns : " +col.size()); 
        // Number of rows 
        List <WebElement> rows = driver.findElements(By.xpath("/html/body/table/tbody/tr/td[1]")); 
        System.out.println("No of rows : " + rows.size());

Code Explanation:

We declared the selenium webdriver object “driver” and initialized it to chrome driver, and used the ‘List<webelement>’ list of web element datatype to find the number of columns and rows.

To set up the chrome driver using selenium refer to this article How to open chrome browser using Selenium in Java




My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!