How to Handle Dynamic Web Tables using Selenium WebDriver in Java?


Handling dynamic web tables is often one of the biggest obstacles when it comes to web automation testing, making use of Selenium WebDriver an efficient means for automating browsers. Although its powerful feature set enables interaction with website elements, dealing with dynamic tables requires additional techniques aimed at effectively extracting and locating data efficiently; in such instances Java provides various approaches that help handle dynamic tables seamlessly.

With Selenium WebDriver in Java's adaptability and capabilities, testers are able to utilize dynamic web tables effectively and accurately by automating interactions between cells or rows efficiently and precisely. In this tutorial we will cover strategies and best practices for handling dynamic tables using Selenium WebDriver; specifically, regarding their navigation through dynamic tables using Selenium WebDriver efficiently and precisely.

Selenium WebDriver

Selenium WebDriver is an increasingly popular Java library designed for automating web browsers. With powerful APIs to interact with website elements and perform actions as well as gather information from pages, Selenium WebDriver offers powerful solutions for automating browser sessions.

To use Selenium WebDriver with Java, first assemble an appropriate driver executable for each web browser (Chrome/Firefox etc) as well as add Selenium dependency into your project.

Once configured, you can create an instance of the WebDriver interface and launch a web browser window. From here, you can navigate to various URLs using methods such as findElement() and sendKeys() as well as perform actions like clicking, submitting forms or extracting data.

WebDriver driver = new ChromeDriver();

Approaches

Here are several methods for handling dynamic web tables using Selenium WebDriver in Java.

  • Using XPath

  • Using CSS Selector

Approach1: Using XPath

XPath is an elegant yet powerful language for finding elements in XML documents, including websites. When working with dynamic web tables using Selenium WebDriver in Java, XPath can prove particularly valuable.

To effectively handle dynamic web tables, first conduct an examination of its structure and identify individual attributes or patterns that you can leverage as building blocks of an XPath expression. With it you can make use of functions like position(), contains() or starts-with() in order to handle changing rows or columns on varying webpages.

Once your XPath expression is complete, WebDriver offers findElement() or findElements() methods which use that expression as their locator and allow for efficient interaction with its contents.

Algorithm

  • Locate and identify the structure and elements you wish to access of a dynamic web table.

  • Examine any unique attributes or patterns that can help create an XPath expression to locate desired elements.

  • Create the XPath expression based on identified attributes or patterns.

  • Instantiate a WebDriver instance and navigate to the website hosting a dynamic table.

  • Use the WebDriver findElements() method, passing in an XPath expression as locator.

  • Retrieve elements matching an XPath expression as a list.

  • Navigating through a list of elements allows users to gain access to desired data or perform desired actions.

  • Handle any exceptions or errors that arise throughout the process.

  • Once finished working with your dynamic web table, close down WebDriver to close off.

Example

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

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

      // Create ChromeDriver instance
      WebDriver driver = new ChromeDriver();

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

      // Locate the table element
      WebElement table = driver.findElement(By.xpath (//*[@id="post-body-1325137018292710854"]/div[1]/div[1]/div[5]/table/thead/tr/th[1]"));

      // Find all rows in the table
      List<WebElement> rows = table.findElements(By.xpath(".//tr"));

      // Iterate through each row and print cell values
      for (WebElement row : rows) {
         List<WebElement> cells = row.findElements(By.xpath(".//td"));
         for (WebElement cell : cells) {
            String cellText = cell.getText();
            System.out.println(cellText);
         }
      }

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

Note that "path/to/chromedriver" must be replaced with the actual location of ChromeDriver executable on your system.

Output

Structure
Country
City
Height
Built	
Rank
…
Burj Khalifa	
UAE	
Dubai	
829m	
2010	
1	
Clock Tower Hotel	
Saudi Arabia	
Mecca	
601m	
2012	
2	
Taipei 
101	
Taiwan	
Taipei	
509m	
2004	
3	
Financial Center	
China	
Shanghai	
492m	
2008	
4	

Approach2: Using CSS Selector

CSS selectors provide an effective means for quickly locating elements when working with dynamic web tables using Selenium WebDriver in Java, through analysis. By looking closely at the structure of a table you can identify specific attributes or patterns which you can target using CSS selectors - for instance class names, element types or attribute values can all help! To access elements with ease.

WebDriver's findElement() or findElements() methods provide an efficient means of finding elements within dynamic web tables by passing in CSS selectors as locators elements and allow you to efficiently interact with desired elements.

Algorithm

  • Utilize CSS selectors to target unique attributes or patterns within a dynamic web table's structure.

  • Create a CSS selector based on identified attributes or patterns.

  • Initialise the WebDriver and navigate to the website with dynamic web table.

  • Utilize the WebDriver findElements() method with your CSS selector as locator to use its findElements() feature.

  • Retrieve a list of elements matching the CSS selector as quickly and efficiently as possible.

  • Navigate through a list of elements to access desired information or take desired steps.

  • Address any exceptions or errors that arise during the process.

  • Once finished with your work on the dynamic web table, close down WebDriver.

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

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

      // Create ChromeDriver instance
      WebDriver driver = new ChromeDriver();

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

      // Locate the table element
      WebElement table = driver.findElement(By.cssSelector("tsc_table_s13"));

      // Find all rows in the table
      List<WebElement> rows = table.findElements(By.cssSelector("tr"));

      // Iterate through each row and print cell values
      for (WebElement row : rows) {
         List<WebElement> cells = row.findElements(By.cssSelector("td"));
         for (WebElement cell : cells) {
            String cellText = cell.getText();
            System.out.println(cellText);
         }
      }

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

Output

Structure
Country
City
Height
Built	
Rank
…
Burj Khalifa	
UAE	
Dubai	
829m	
2010	
1	
Clock Tower Hotel	
Saudi Arabia	
Mecca	
601m	
2012	
2	
Taipei 
101	
Taiwan	
Taipei	
509m	
2004	
3	
Financial Center	
China	
Shanghai	
492m	
2008	
4

Conclusion

In this tutorial, we have seen that knowing how to manage dynamic web tables using Selenium WebDriver in Java is a vital skill for web automation and data extraction tasks. By employing locators such as XPath or CSS Selector, developers can locate table elements quickly and navigate rows and columns efficiently in order to extract relevant data.

Dynamic web tables make it possible to efficiently process large volumes of information and take appropriate actions based on specific conditions, extracting valuable data for further analysis, storage or validation purposes. With proper understanding and execution of Selenium WebDriver in Java developers are able to effectively handle dynamic tables while automating various workflows using Selenium WebDriver - automate data-driven workflows through Selenium WebDriver.

Updated on: 25-Jul-2023

521 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements