---Advertisement---

Mastering Dynamic Web Tables in Selenium WebDriver: A Complete Guide-2025

By Shiva

Updated On:

---Advertisement---
Mastering Dynamic Web Tables in Selenium WebDriver: A Complete Guide-2025
  • “Learn to handle dynamic web tables in Selenium WebDriver like a pro with this in-depth guide!”
  • “Master XPath and CSS selectors to efficiently locate and interact with dynamic tables.”
  • “Automate data extraction, row selection, and validation for dynamic tables in Selenium.”

Handling web tables in Selenium WebDriver is a crucial skill for automated testing. Web tables can be classified into two types:

  1. Static Tables: The number of rows and columns remains fixed.
  2. Dynamic Tables: The number of rows and columns can change based on user input or other factors.

While handling static tables is relatively simple, dealing with dynamic web tables can be more complex. This guide will help you understand how to interact with dynamic tables using Selenium WebDriver efficiently.


Before diving into handling web tables, let’s understand what web elements are. Web elements are HTML elements such as textboxes, dropdowns, radio buttons, and submit buttons. Each of these elements is defined with opening and closing tags.

<p> My First HTML Document </p>

A web table in HTML is defined using the <table> tag, with rows (<tr>) and columns (<td> or <th>).


To interact with a dynamic web table, you need to locate its elements first. The most effective way to do this is by using XPath.

  1. Open Chrome and go to Demo Web Table.
  2. Right-click on the web table element you want to inspect (e.g., the Company column header).
  3. Select Inspect from the menu. This will open Chrome DevTools.
  4. Right-click the highlighted element, select Copy, and then choose Copy XPath.
  5. Use the copied XPath in Selenium WebDriver.
//*[@id="leftcontainer"]/table/thead/tr/th[1]

  • “Enhance your Selenium skills with real-world examples of handling web tables dynamically.”
  • “Boost your test automation efficiency by mastering dynamic web tables in Selenium WebDriver.”
  • “Optimize your Selenium scripts with advanced techniques for handling dynamic tables effortlessly.”

You can extract data from a web table using findElements() in Selenium WebDriver.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;

public class WebTableExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.get("https://demo.guru99.com/test/web-table-element.php");
        
        // Locate table
        WebElement table = driver.findElement(By.xpath("//*[@id='leftcontainer']/table"));
        
        // Get all rows
        List<WebElement> rows = table.findElements(By.tagName("tr"));
        
        // Iterate over rows
        for (WebElement row : rows) {
            List<WebElement> cols = row.findElements(By.tagName("td"));
            for (WebElement col : cols) {
                System.out.print(col.getText() + " \t ");
            }
            System.out.println();
        }
        
        driver.quit();
    }
}

If you need to click on a specific cell within a table, use XPath dynamically.

WebElement specificCell = driver.findElement(By.xpath("//*[@id='leftcontainer']/table/tbody/tr[3]/td[2]"));
specificCell.click();

You can loop through the table and select a row that matches a given condition (e.g., selecting a company with a specific name).

List<WebElement> rows = driver.findElements(By.xpath("//*[@id='leftcontainer']/table/tbody/tr"));
for (WebElement row : rows) {
    WebElement companyName = row.findElement(By.xpath("td[1]"));
    if (companyName.getText().equals("Infosys")) {
        System.out.println("Company Found: " + companyName.getText());
        row.findElement(By.xpath("td[2]")); // Perform actions if needed
        break;
    }
}

Many web applications display data across multiple pages. To handle pagination, you need to interact with the Next button and extract data from all available pages.

while (true) {
    List<WebElement> rows = driver.findElements(By.xpath("//*[@id='leftcontainer']/table/tbody/tr"));
    for (WebElement row : rows) {
        System.out.println(row.getText());
    }
    
    WebElement nextButton = driver.findElement(By.xpath("//a[text()='Next']"));
    if (nextButton.isEnabled()) {
        nextButton.click();
    } else {
        break;
    }
}

  1. Use Dynamic XPath: Avoid absolute XPath, and prefer relative XPath using attributes.
  2. Wait for Elements to Load: Use WebDriverWait to ensure elements are available before interacting.
  3. Handle Stale Element Exceptions: Web tables change dynamically; handle exceptions accordingly.
  4. Optimize Loops for Large Tables: Extract only necessary data to improve script efficiency.

Handling dynamic web tables in Selenium WebDriver is essential for effective test automation. By using XPath, handling pagination, and implementing best practices, you can efficiently interact with any web table, regardless of its complexity.

Leave a Comment

Index