我下载了所有必要的罐子并运行了这个测试程序:
public void getElements() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("https://www.facebook.com/");
final HtmlDivision div = page.getHtmlElementById("_li");
//final HtmlAnchor anchor = page.getAnchorByName("anchor_name");
webClient.closeAllWindows();
} 但由于错误,它每次都会退出: דצמ09,2014
2:04:10 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
דצמ 09, 2014 2:04:11 PM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
WARNING: Obsolete content type encountered: 'application/x-javascript'.
Exception in thread "main" com.gargoylesoftware.htmlunit.ElementNotFoundException: elementName=[*] attributeName=[id] attributeValue=[_li]
at com.gargoylesoftware.htmlunit.html.HtmlPage.getElementById(HtmlPage.java:1729)
at com.gargoylesoftware.htmlunit.html.HtmlPage.getHtmlElementById(HtmlPage.java:1679)
at DmozSpider.main(DmozSpider.java:16)
答案 0 :(得分:0)
在第
行page.getHtmlElementById("_li");
代码抛出ElementNotFoundException
,因为DOM没有任何ID为“_li”的XML元素。正如Javadoc of ElementNotFoundException所述:
在DOM模型中找不到指定的XML元素时抛出的异常。
如果您要选择带class="_li"
的div标签,则应使用
page.getByXPath("//div[@class='_li']");