我下载了所有必要的jars,并运行了以下测试程序:
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)发布于 2014-12-09 20:10:25
在队伍中
page.getHtmlElementById("_li");代码抛出一个ElementNotFoundException,因为DOM没有任何id为"_li“的XML元素。正如Javadoc of ElementNotFoundException所说的:
当在DOM模型中找不到指定的XML元素时引发的异常。
如果您希望使用class="_li"选择一个div标记,那么您应该使用
page.getByXPath("//div[@class='_li']");https://stackoverflow.com/questions/27378457
复制相似问题