Similar to other element types that we have seen in earlier sections, an HTML table is represented as WebElement.
However, there are no table-specific methods that are available with the WebElement
interface.
While working with tables, we can find the rows and cells effectively by using a set of the By
class methods. In this recipe, we will see how to get rows and columns in table.
Let's create a simple test that will print data from a table, as shown in the following code example:
@Test public void testWebTable() { WebElement simpleTable = driver.findElement(By.id("items")); // Get all rows List<WebElement> rows = simpleTable.findElements(By.tagName("tr")); assertEquals(3, rows.size()); // Print data from each row 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(); } }