In previous tutorial, we have discussed about how to find 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 number of rows and columns of Dynamic Web Table.

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 TableDemo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = newChromeDriver();
driver.get("https://www.seleniumeasy.com/test/table-search-filter-demo.html");
// To find no of columns
List ColumnList = driver.findElements(By.xpath("//*[@id='task-table']/thead/tr/th"));
System.out.println("No of Columns are :"+ColumnList.size());
//To find no of Rows
List RowList = driver.findElements(By.xpath("//*[@id='task-table']/tbody/tr")); System.out.println("No of Rows are :"+RowList.size());
driver.close();
}
}
Output
No of Columns are :4
No of Rows are :7

Here,
1. findElements commands returns a list of ALL the elements matching the specified locator
2. The count of column is found using XPath – (“//*[@id=’task-table’]/thead/tr/th”)
3. The count of rows is found using XPath – (“//*[@id=’task-table’]/tbody/tr”)
How to get all the values from a Dynamic Table
In the above table, there are 4 columns and 7 rows and we want to get the data from each cell.
Here,
1. Fetch the XPath of the table by using – By.xpath(“/html/body/div[2]/div/div[2]/div[1]/div/table/tbody”
2. Rows are located by using – mytable.findElements(By.tagName(“tr”))
3. RowListCount give the total number of rows
4. For each row, we have fetched total number of columns by using RowList.get(row).findElements(By.tagName(“td”));
5. Iterate through each column of each row and fetch the data
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 DynamicTable_DataExtract {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\Vibha\\Desktop\\Drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = newChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.seleniumeasy.com/test/table-search-filter-demo.html");
//To locate Table
WebElement mytable = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div[1]/div/table/tbody"));
//To locate rows of the table
List RowList = mytable.findElements(By.tagName("tr"));
//To find no of Rows
int RowListCount = RowList.size();
System.out.println("No of Rows are :"+RowListCount);
//This loop will go till last row in the table
for (int row = 0; row < RowListCount; row++)
{
// To locate columns of that specific row
List ColumnList = RowList.get(row).findElements(By.tagName("td"));
//To calculate no of columns in that specific row
int ColumnsListCount = ColumnList.size();
System.out.println("No of cells in Row "+row+" are "+ColumnsListCount);
//This loop will execute till last column in the specific row
for (int column = 0; column < ColumnsListCount; column++)
{
// To retrieve text from that specific cell
String celtext = ColumnList.get(column).getText();
System.out.println("Cell Value of row number " + row + " and column number " + column + " - " + celtext);
}
System.out.println("-------------------------------------------------- ");
}
driver.close();
}
}
Output
No of Rows are :7
No of cells in Row 0 are 4
Cell Value of row number 0 and column number 0 - 1
Cell Value of row number 0 and column number 1 - Wireframes
Cell Value of row number 0 and column number 2 - John Smith
Cell Value of row number 0 and column number 3 - in progress
--------------------------------------------------
No of cells in Row 1 are 4
Cell Value of row number 1 and column number 0 - 2
Cell Value of row number 1 and column number 1 - Landing Page
Cell Value of row number 1 and column number 2 - Mike Trout
Cell Value of row number 1 and column number 3 - completed
--------------------------------------------------
No of cells in Row 2 are 4
Cell Value of row number 2 and column number 0 - 3
Cell Value of row number 2 and column number 1 - SEO tags
Cell Value of row number 2 and column number 2 - Loblab Dan
Cell Value of row number 2 and column number 3 - failed qa
--------------------------------------------------
No of cells in Row 3 are 4
Cell Value of row number 3 and column number 0 - 4
Cell Value of row number 3 and column number 1 - Bootstrap 3
Cell Value of row number 3 and column number 2 - Emily John
Cell Value of row number 3 and column number 3 - in progress
--------------------------------------------------
No of cells in Row 4 are 4
Cell Value of row number 4 and column number 0 - 5
Cell Value of row number 4 and column number 1 - jQuery library
Cell Value of row number 4 and column number 2 - Holden Charles
Cell Value of row number 4 and column number 3 - deployed
--------------------------------------------------
No of cells in Row 5 are 4
Cell Value of row number 5 and column number 0 - 6
Cell Value of row number 5 and column number 1 - Browser Issues
Cell Value of row number 5 and column number 2 - Jane Doe
Cell Value of row number 5 and column number 3 - failed qa
--------------------------------------------------
No of cells in Row 6 are 4
Cell Value of row number 6 and column number 0 - 7
Cell Value of row number 6 and column number 1 - Bug fixing
Cell Value of row number 6 and column number 2 - Kilgore Trout
Cell Value of row number 6 and column number 3 - in progress
--------------------------------------------------

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