Java HtmleElement不显示子节点,但HtmleElement.asXml()方法显示

Java HtmleElement不显示子节点,但HtmleElement.asXml()方法显示,java,htmlunit,Java,Htmlunit,我有一个保存在HtmlPage对象中的网页。我应用了XPath,其结果存储在列表中 List<?> items = null; items = page.getByXPath("//div[contains(@class,'search-result-cards')]/div[contains(@class,'listContainer')]"); 点表示这些节点继续运行,因为输出非常大。 如果需要我可能没有提到的任何其他信息,请告诉我。.toString()只打印当前的domeE

我有一个保存在HtmlPage对象中的网页。我应用了XPath,其结果存储在列表中

List<?> items = null;
items = page.getByXPath("//div[contains(@class,'search-result-cards')]/div[contains(@class,'listContainer')]");
点表示这些节点继续运行,因为输出非常大。 如果需要我可能没有提到的任何其他信息,请告诉我。

.toString()
只打印当前的domeElement,而不打印子元素

您需要使用XPath获取子对象,如:

List<HtmlElement> items = page.getByXPath("//div[contains(@class,'listContainer')]");
for (HtmlElement item : items) {
    List<HtmlElement> children = item.getByXPath(".//div");
    for (HtmlElement child : children) {
        System.out.println(child);
    }
}

toString()方法似乎不返回元素的内容。为什么这是一个问题?这不是问题,我的代码的下一段使用这个html元素对象进行解析,其中它希望输入应该是asXml方法的输出,但仅限于HtmleElement类型。什么是“仅限于HtmleElement类型?”字符串idStr=ParseUtil.parse(HtmleElementObject);这是最终将被调用的方法,其方法签名为publicstaticstringparse(HtmlElement元素)。HtmlElement包含子元素(否则,asXml()将不包含它们);您遇到的问题是toString()方法。
Output
Printing just the element ::: HtmlDivision[<div class="listContainer" data-ptitle="3139847000" data-reactid="402">]

he.asXml() Output
<div class="listContainer" data-ptitle="3139847000" data-reactid="402">
<div class="imageContainer" data-reactid="403">
<div class="prodInfoContainer" data-reactid="406">
.
.
.
List<HtmlElement> items = page.getByXPath("//div[contains(@class,'listContainer')]");
for (HtmlElement item : items) {
    List<HtmlElement> children = item.getByXPath(".//div");
    for (HtmlElement child : children) {
        System.out.println(child);
    }
}
for (HtmlElement child : item.getHtmlElementDescendants()) {
    System.out.println(child);
}