使用HtmlUnit在DomNode上调用getByXPath

时间:2011-07-09 09:30:13

标签: java htmlunit

我使用getByXPath从HTMLPage对象中提取了一个DomNode对象列表。但是,当我尝试查询返回的DomNode对象时,再次使用getByXPath,结果就好像我查询了HTMLPage对象。

final WebClient webClient = new WebClient();
HTMLPage page = (HtmlPage) webClient.getPage("http://test.page.com");

final List<HtmlTableCell> cells = (List<HtmlTableCell>) page.getByXPath("//td[@width='85%']");

// Number of cells: 16
logger.debug("Number of cells: " + cells.size());

HtmlTableCell cell = cells.get(0);
final List<HtmlTableCell> childCells = (List<HtmlTableCell>) cell.getByXPath("//td[@width='85%']");

// Number of cells: 16
logger.debug("Number of cells: " + childCells.size());

1 个答案:

答案 0 :(得分:1)

XPath中的

//td表示选择所有td元素,无论它们在文档中的位置。见http://www.w3schools.com/xpath/xpath_syntax.asp