How to get all the values from a Dynamic Table in Selenium WebDriver


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class TableDemo {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\Vibha\\Automation\\Chrome\\chromedriver\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();
        driver.get("https://www.techlistic.com/p/demo-selenium-practice.html");

        driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

        // To find no of columns
        List ColumnList = driver.findElements(By.xpath("//*[@id='customers']/tbody/tr/th"));
        System.out.println("No of Columns are :"+ColumnList.size());

        //To find no of Rows, first row is heading
        List RowList = driver.findElements(By.xpath("//*[@id='customers']/tbody/tr"));
        System.out.println("No of Rows are :"+RowList.size());
        driver.close();
    }

}


1. findElements command returns a list of ALL the elements matching the specified locator.
2. The count of columns is found using XPath – (“//*[@id=’customers’]/tbody/tr/th”)
3. The count of rows is found using XPath – (“//*[@id=’customers’]/tbody/tr”)

How to get all the values from a Dynamic Table

In the above table, there are 3 columns and 7 rows, and we want to get the data from each cell. First row is heading, so we want to find the data of 6 rows only.

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import java.util.concurrent.TimeUnit;
    
    public class DynamicTable_DataExtract {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver","C:\\Users\\Vibha\\Automation\\Chrome\\chromedriver\\chromedriver.exe");
    
            WebDriver driver = new ChromeDriver();
            driver.get("https://www.techlistic.com/p/demo-selenium-practice.html");
    
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    
            //Get number of rows In table.
            int Row_Count = driver.findElements(By.xpath("//*[@id='customers']/tbody/tr")).size();
            System.out.println("No of Rows are :"+Row_Count);
    
            //Get number of columns In table.
            int Column_Count = driver.findElements(By.xpath("//*[@id='customers']/tbody/tr/th")).size();
            System.out.println("No of Columns are :"+Column_Count);
    
            //divided xpath In three parts to pass Row_count and Col_count values.
            String first_part = "//*[@id='customers']/tbody/tr[";
            String second_part = "]/td[";
            String third_part = "]";
    
    
            //Used for loop for number of rows.
            for (int i=2; i<=Row_Count; i++){
    
                //Used for loop for number of columns.
                for(int j=1; j<=Column_Count; j++){
    
                    //Prepared final xpath of specific cell as per values of i and j.
                    String final_xpath = first_part+i+second_part+j+third_part;
    
                    //Will retrieve value from located cell and print It.
                    String Table_data = driver.findElement(By.xpath(final_xpath)).getText();
                    System.out.print(Table_data +" ");
                }
    
                System.out.println("");
                System.out.println("---------------------------------");
            }
    
           driver.close();
        }
    }
    

    2 thoughts on “How to get all the values from a Dynamic Table in Selenium WebDriver

    1. I'm new to selenium. I was looking for code of dynamic table from sometime as I have this requirement in my project. This has helped me a lot. Thanks. Hope to see many more useful blogs

      Like

    Leave a comment