I would like access all tables iterating in a web page with unknown numbers of tables. I have written this code
import java.io.*;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
import com.gargoylesoftware.htmlunit.html.*;
import com.gargoylesoftware.htmlunit.WebClient;
public class test {
public static void main(String[] args) throws Exception {
WebClient client = new WebClient();
HtmlPage currentPage = client.getPage("http://www.mysite.com");
client.waitForBackgroundJavaScript(10000);
FileWriter fstream = new FileWriter("index.txt");
BufferedWriter out = new BufferedWriter(fstream);
for (int i=0;i<2;i++){
final HtmlTable table =(HtmlTable) currentPage.getByXPath("//table").get(i);
for (final HtmlTableRow row : table.getRows()) {
for (final HtmlTableCell cell : row.getCells()) {
out.write(cell.asText()+',');
}
out.write('\n');
}
}
out.close();
client.closeAllWindows();
}
}
i have tried with checking condition:
while(currentPage.getByXPath("//table")){....}
but is not accepted. What is the correct checking condition?
htmlunit.html.HtmlPage has a method getElementsByTagName(String tagName)
where you can pass it the tagName of "table". Then just get the length of how many it returns, in rough pseudocode:
var x = getElementsByTagName("table");
var nTables = x.length