Working with Web Tables in Selenium
Web tables are a common component of web applications, often used to display structured data like search results, reports, or inventory lists. For testers using Selenium, working with web tables is an essential skill. Selenium WebDriver provides tools to interact with tables, read data, and verify its accuracy.
What Is a Web Table?
A web table is an HTML element that organizes data into rows (<tr>) and columns (<td> or <th>), typically wrapped in a <table> tag. These tables can be static (unchanging content) or dynamic (data loaded or updated through JavaScript).
Locating a Table
To begin working with a web table, you need to locate it using Selenium’s findElement() or findElements() methods. For example:
WebElement table = driver.findElement(By.id("exampleTable"));
You can also use XPath or CSS Selectors if the table lacks a unique ID.
Counting Rows and Columns
To find the number of rows:
List<WebElement> rows = table.findElements(By.tagName("tr"));
System.out.println("Number of rows: " + rows.size());
To find the number of columns (usually from the first row):
List<WebElement> columns = rows.get(0).findElements(By.tagName("td"));
System.out.println("Number of columns: " + columns.size());
Reading Cell Data
You can loop through each row and column to extract cell values:
for (WebElement row : rows) {
List<WebElement> cells = row.findElements(By.tagName("td"));
for (WebElement cell : cells) {
System.out.print(cell.getText() + "\t");
}
System.out.println();
}
This approach is useful for verifying table data or extracting specific values during testing.
Working with Dynamic Tables
Dynamic tables may load content after page load. In such cases:
Use WebDriverWait to ensure the table is fully loaded.
Handle pagination or filters if present.
Use relative XPath to locate dynamic elements.
Example:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicTable")));
Conclusion
Mastering web table interaction in Selenium is vital for robust test automation. By understanding how to locate, read, and verify table content, testers can ensure their web applications are displaying accurate and functional data. Whether static or dynamic, Selenium provides flexible ways to handle web tables efficiently.
Learn Selenium Java Training in Hyderabad
Read More:
Taking Screenshots with Selenium in Java
How to Run Selenium Tests in Headless Mode
Validating Broken Links Using Selenium
Selenium Test Automation Best Practices in Java
Visit our IHub Talent Training Institute
Comments
Post a Comment