Last Updated On
In the previous tutorial, we discussed about how to find the XPath of Dynamic Web Table. In this tutorial, we will see how we can get all the data from Dynamic Web Table.
What is Dynamic Table – A table where data is dynamic, i.e. Number of rows and columns are NOT fixed. Below is an example to fetch the number of rows and columns of a Dynamic Web Table.

Let us create a program to find the number of rows and columns in the table.
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();
}
}
The output of the above program is

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.
We have created a custom xpath for table row and column, and we replace the row and column index with variables. So, that we can iterate over every table row’s td (cell) using for loop. We have used first for loop row count, which will iterate over every row. And second loop will iterate over every table data (td) of each row one by one.
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();
}
}
The output of the above program is

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!
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
LikeLike
Thank You Vivek
LikeLike