Use Tables
The first set of examples will use this simple html.
<html><head><title>Table sample</title></head><body> <table id="table1"> <tr> <th>Number</th> <th>Description</th> </tr> <tr> <td>5</td> <td>Bicycle</td> </tr> </table> </body></html>
This example shows how to iterate over all the rows and cells
final HtmlTable table = page.getHtmlElementById("table1"); for (final HtmlTableRow row : table.getRows()) { System.out.println("Found row"); for (final HtmlTableCell cell : row.getCells()) { System.out.println(" Found cell: " + cell.asNormalizedText()); } }
The following sample shows how to access specific cells by row and column
final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://foo.com"); final HtmlTable table = page.getHtmlElementById("table1"); System.out.println("Cell (1,2)=" + table.getCellAt(1,2));
More complex table
The next examples will use a more complicated table that includes table header, footer and body sections as well as a caption
<html><head><title>Table sample</title></head><body> <table id="table1"> <caption>My complex table</caption> <thead> <tr> <th>Number</th> <th>Description</th> </tr> </thead> <tfoot> <tr> <td>7</td> <td></td> </tr> </tfoot> <tbody> <tr> <td>5</td> <td>Bicycle</td> </tr> </tbody> <tbody> <tr> <td>2</td> <td>Tricycle</td> </tr> </tbody> </table> </body></html>
HtmlTableHeader, HtmlTableFooter and HtmlTableBody sections are groupings of rows. There can be at most one header and one footer but there may be more than one body. Each one of these contains rows which can be accessed via getRows()
final HtmlTableHeader header = table.getHeader(); final List<HtmlTableRow> headerRows = header.getRows(); final HtmlTableFooter footer = table.getFooter(); final List<HtmlTableRow> footerRows = footer.getRows(); for (final HtmlTableBody body : table.getBodies()) { final List<HtmlTableRow> rows = body.getRows(); ... }
Every table may optionally have a caption element which describes it.
final String caption = table.getCaptionText()