Get Data From Non-Table HTML Tag WebTable Using Selenium WebDriver

We have seen multiple posts on handling a web table in Selenium WebDriver. You can find them below:-

Storing Web Table Data Into List Of Map – Java

Storing Web Table Data Into A List Of Maps Using Java Stream APIs

Storing Web Table With Pagination Data Into List Of Map – Java

All above examples include a table created using table, thead, tbody, tr and td HTML tags. Many asked that in real-time we get non table tag web table also so provide an example of that as well.

Yeah, It’s true it is not mandatory to create a web table with table HTML tag only. It can be created using other tags as well. But the major part is that the handling mechanism is not different than the table tag. Just you need to play with locators.

I have taken a web table example developed using angular. I have performed below actions on this web table:-

  1. Get all headers and print.
  2. Get all rows excluding headers and print data of each column of a row with header name.

Selenium WebDriver Code

package WebTablesExamples;

import java.time.Duration;
import java.util.ArrayList;
import java.util.LinkedHashMap;
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;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.github.bonigarcia.wdm.WebDriverManager;

public class NonTableTrTdWebTable {

	public static void main(String[] args) {

		WebDriverManager.chromedriver().setup();
		WebDriver driver = new ChromeDriver();
		driver.get("http://demo.automationtesting.in/WebTable.html");
		
		// Wait till web table is loaded
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
		wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("//div[contains(@class,'ui-grid-viewport')]//div[contains(@class,'grid-row')]"), 1));

		// Let's get header first
		String headerLoc = "//div[contains(@class,'header-cell')]//span[contains(@id,'header-text')]";
		List allHeadersEle = driver.findElements(By.xpath(headerLoc));
		List allHeaderNames = new ArrayList();
		for (WebElement header : allHeadersEle) {
			String headerName = header.getText();
			allHeaderNames.add(headerName);
		}
		System.out.println("Headers are:-");
		System.out.println(allHeaderNames);

		// Each row will be a key value pair. So we will use LinkedHashMap so that order
		// can be retained.
		// All map will be added to a list.
		List> allTableData = new ArrayList>();

		// Get total rows count excluding headers
		String rowLoc = "//div[contains(@class,'ui-grid-viewport')]//div[contains(@class,'grid-row')]";
		List allRowsEle = driver.findElements(By.xpath(rowLoc));
		// Starting from 1 as xpath index starts from 1
		for (int i = 1; i <= allRowsEle.size(); i++) {
			// Getting specific row with each iteration
			String specificRowLoc = "(//div[contains(@class,'ui-grid-viewport')]//div[contains(@class,'grid-row')])["+i+"]";
			// Locating only cells of specific row.
			List allColumnsEle = driver.findElement(By.xpath(specificRowLoc)).findElements(By.xpath(".//div[contains(@class,'grid-cell-contents')]"));
			// Creating a map to store key-value pair data. It will be created for each iteration of row
			LinkedHashMap eachRowData = new LinkedHashMap<>();
			// Iterating each cell
			for (int j = 0; j < allColumnsEle.size(); j++) {
				// Getting cell value
				String cellValue = allColumnsEle.get(j).getText();
				// We will put in to map with header name and value with iteration
				// Get jth index value from allHeaderNames and jth cell value of row
				eachRowData.put(allHeaderNames.get(j), cellValue);
			}
			// After iterating row completely, add in to list.
			allTableData.add(eachRowData);

		}

		System.out.println("Table data are:-");
		System.out.println("=========================================");
		for(LinkedHashMap data : allTableData)
		{
			for(String key : data.keySet())
			{
				System.out.println(key + "      : "+ data.get(key));
			}
			System.out.println("=========================================");
		}
		driver.quit();
	}
}

Output

Headers are:-
[Email, First Name, Gender, Last Name, Phone, Action]
Table data are:-
=========================================
Email      : aaaaaaaadimim@gmail.com
First Name      : Faculdade
Gender      : FeMale
Last Name      : Fadergs
Phone      : 1523611667
=========================================
Email      : aaaaaaaaadimim@gmail.com
First Name      : Faculdade
Gender      : FeMale
Last Name      : Fadergs
Phone      : 1523611867
=========================================
Email      : batata1958@gmail.com
First Name      : Faculdade
Gender      : FeMale
Last Name      : Fadergs
Phone      : 9999968005
=========================================
Email      : admin@fadergs.edu.br
First Name      : Faculdade
Gender      : FeMale
Last Name      : Fadergs
Phone      : 0007678678
=========================================
Email      : admin5@fadergs.edu.br
First Name      : Faculdade
Gender      : FeMale
Last Name      : Fadergs
Phone      : 0007678678
=========================================
Email      : admin14@fadergs.edu.br
First Name      : Faculdade
Gender      : FeMale
Last Name      : Fadergs
Phone      : 0007678678
=========================================
Email      : admin15@fadergs.edu.br
First Name      : Faculdade
Gender      : FeMale
Last Name      : Fadergs
Phone      : 0007678678
=========================================
Email      : tyutyutyudu@testando.com
First Name      : Faculdade
Gender      : FeMale
Last Name      : Fadergs
Phone      : 8523677452
=========================================
Email      : admin@fadergs.edououoou.br
First Name      : Faculdade
Gender      : Male
Last Name      : Fadergs
Phone      : 8882300678
=========================================
Email      : admin@fadergs.edu.br
First Name      : Faculdade
Gender      : FeMale
Last Name      : Fadergs
Phone      : 0007678678
=========================================

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Leave a Reply

Your email address will not be published. Required fields are marked *