How to Handle Static Web Tables using Selenium WebDriver using Java?


To handle static web tables with Selenium WebDriver in Java, a series of procedures must be followed to extract relevant data and operate on the table components. The initial step involves locating the table present on the webpage using suitable identifiers. Once located, individual rows and columns are accessed via HTML tags such as <tr> and <td>.

The data from web tables can be extracted and stored for further processing by iteratively scanning each row and column. Additionally, actions such as clicking on specific cells or verifying the presence of certain data in the table can also be performed. By utilizing Se-lenium WebDriver and Java, static web tables become more efficiently managed with automation.

Web Tables

When working with web tables in Java using Selenium WebDriver, one must interact with HTML tables on web pages. To locate the table element appropriately, use suitable locators. Once you have located the table, retrieve all its rows by using the `findElements()` method and iterate through them via a loop. Access each column of each row by employing the `findElements()` method again within this loop. The desired data from each column can then be extracted via methods like `getText()`, or `getAttribute()’.

WebDriver driver = new ChromeDriver();

Approaches

There are multiple techniques available to handle static web tables using Selenium WebDriver in Java. The following approaches can be utilized:

  • Using HTML Table Structure

  • Using XPath Axes

Using HTML Table Structure

To handle static web tables using Selenium WebDriver and Java, you can utilize the HTML table structure method. Start by identifying the table element using its unique identifier or any relevant HTML attributes. Once located, you can extract the table rows and columns using WebDriver commands and iterate through them as needed. Retrieve specific cell values by referencing their row and column indexes.

Additionally, you can perform table-related actions such as sorting by column, filtering, or searching for particular data. By leveraging WebDriver's capabilities and Java programming, you can effectively interact with static web tables, extract data, and perform various operations seamlessly.

Algorithm

  • Launch the web browser using WebDriver.

  • Navigate to the desired webpage containing the static web table.

  • Locate the table element using appropriate WebDriver commands (e.g., by ID, class, XPath, etc.).

  • Extract the table rows by finding all "tr" elements within the table.

  • Iterate through the rows using a loop.

  • Within each row, extract the table cells ("td" elements) or header cells ("th" elements) as required.

  • Perform desired operations on the cell data (e.g., retrieve text, verify values, etc.).

  • Optionally, perform additional actions like sorting, filtering, or searching on the table.

Example

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

public class TableExample {
   public static void main(String[] args) {
      // Set up WebDriver (Assuming ChromeDriver here)
      System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
      WebDriver driver = new ChromeDriver();

      // Navigate to the desired webpage
      driver.get("https://www.techlistic.com/2017/02/automate-demo-web-table-with-selenium.html");

      // Find the table element
      WebElement tableElement = driver.findElement(By.tagName("table"));
      List<WebElement> rows = tableElement.findElements(By.tagName("tr"));

      // Iterate through each row
      for (WebElement rowElement : rows) {
         List<WebElement> cells = rowElement.findElements(By.tagName("td"));

         // Iterate through each cell in the row
         for (WebElement cellElement : cells) {
            String cellData = cellElement.getText();
            // Process the cell data as needed
            System.out.print(cellData + "\t");
         }

         // Move to the next line after processing each row
         System.out.println();
      }

      // Close the browser
      driver.quit();
   }
}

Output

Google   Maria Anders        Germany
Meta     Francisco Chang     Mexico
Microsoft    Roland Mendel    Austria
Island Trading    Helen Bennett    UK
Adobe    Yoshi Tannamuri     Canada
Amazon   Giovanni Rovelli     Italy

Using Xpath Axes

To handle static web tables using Selenium WebDriver and Java, you can utilize XPath axes, which provide powerful ways to navigate and interact with table elements. By leveraging XPath axes, you can locate specific rows, columns, or cells within the table structure. The "ancestor," "descendant," and "following-sibling" axes are particularly useful in this context.

For example, to extract table rows, you can use the "//table//tr" XPath expression. To retrieve specific cells within a row, you can combine the row XPath with the "td" axis, such as "//table//tr[position()=2]//td[position()=3]". XPath axes offer flexibility and precision when dealing with complex table structures, enabling you to handle static web tables efficiently and extract the desired data accurately.

Algorithm

  • Launch the web browser using WebDriver.

  • Navigate to the desired webpage containing the static web table.

  • Construct appropriate XPath expressions to locate the table, rows, columns, or cells based on their positions, attributes, or contents.

  • Use XPath axes like "ancestor," "descendant," or "following-sibling" to traverse the table structure and navigate to the desired elements.

  • Extract the necessary data from the table cells using XPath expressions or by combining axes with position or attribute conditions.

  • Process the extracted data as needed (e.g., store it in variables, perform assertions, or output it).

  • Optionally, perform additional operations on the table, such as sorting, filtering, or searching, by adjusting the XPath expressions accordingly.

  • Close the web browser session using WebDriver commands.

Example

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

public class TableExample {
   public static void main(String[] args) {
      // Set up WebDriver (Assuming ChromeDriver here)
      System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
      WebDriver driver = new ChromeDriver();

      // Navigate to the desired webpage
      driver.get("https://www.techlistic.com/2017/02/automate-demo-web-table-with-selenium.html");

      // Retrieve all cells of the table
      List<WebElementa>cells = driver.findElements(By.xpath("//table//tr//td"));

      // Iterate through each cell
      for (WebElement cell : cells) {
         String cellData = cell.getText();
         // Process the cell data as needed
         System.out.print(cellData + "\t");
      }

      // Close the browser
      driver.quit();
   }
}	

Output

Google   Maria Anders        Germany
Meta     Francisco Chang     Mexico
Microsoft    Roland Mendel    Austria
Island Trading    Helen Bennett    UK
Adobe    Yoshi Tannamuri     Canada
Amazon   Giovanni Rovelli     Italy

Conclusion

In this tutorial, we have learned that when working with static web tables using Selenium WebDriver with Java, there are multiple methods available to effectively handle them. The HTML Table Structure method allows you to locate the table element and iterate through rows and cells using suitable locators such as By.tagName(). The XPath Axes method offers flexibility in navigating the HTML structure using XPath expressions to find desired elements. Lastly, CSS Selectors provide an alternative approach to locate and manipulate table elements using CSS selector syntax.

Updated on: 25-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements