我正在使用HtmlUnit抓取一个页面,但它崩溃说它不知道函数“getPropertyValue”
我已经尝试使用waitInBackground函数来查看是否可行,但无济于事。我也尝试了一个不同的网页与JavaScript,它的工作原理。关闭javascript确实有效,但这很重要。
尝试使用其他浏览器不起作用,有时它实际上会给我一个不同的函数名称错误。
public class WebRequest {
public static void main(String[] args) {
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(
java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.OFF);
WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setJavaScriptEnabled(true);
HtmlPage page;
try {
page = webClient.getPage("http://stackoverflow.com/questions/22032343/htmlunit-crashes-on-javascript");
System.out.println(page.getWebResponse().getContentAsString());
System.out.println(page.getTitleText());
} catch (FailingHttpStatusCodeException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
错误堆栈跟踪
Exception in thread "main" ======= EXCEPTION START ========
EcmaError: lineNumber=[4] column=[0] lineSource=[ }] name=[TypeError] sourceName=[http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js] message=[TypeError: Cannot find function getPropertyValue in object [object ComputedCSSStyleDeclaration]. (http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js#4)]
com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function getPropertyValue in object [object ComputedCSSStyleDeclaration]. (http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js#4)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:689)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:621)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:596)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:997)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventListeners(EventListenersContainer.java:179)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:239)
at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:827)
at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:761)
at com.gargoylesoftware.htmlunit.html.HtmlElement$1.run(HtmlElement.java:917)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
at com.gargoylesoftware.htmlunit.html.HtmlElement.fireEvent(HtmlElement.java:922)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1294)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:252)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:462)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:329)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:394)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:379)
at WebRequest.main(WebRequest.java:23)
Caused by: net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot find function getPropertyValue in object [object ComputedCSSStyleDeclaration]. (http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js#4)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3629)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3613)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3634)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3650)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3714)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2233)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2215)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1333)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:411)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:309)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3057)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:103)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:614)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:674)
... 20 more
Enclosed exception:
net.sourceforge.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot find function getPropertyValue in object [object ComputedCSSStyleDeclaration]. (http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js#4)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3629)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3613)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3634)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3650)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3714)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2233)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2215)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1333)
at script(http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:4)
at script(http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:4)
at script.bC(http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2)
at script(http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:4)
at script(http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:4)
at script(http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:4)
at script(http://cdn.sstatic.net/Js/stub.en.js?v=283ea58c715b:8)
at script(http://cdn.sstatic.net/Js/stub.en.js?v=283ea58c715b:11)
at script(http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2)
at script(http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2)
at script(http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2)
at script(http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js:2)
at net.sourceforge.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:798)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:105)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:411)
at com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:309)
at net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3057)
at net.sourceforge.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:103)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$4.doRun(JavaScriptEngine.java:614)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:674)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:621)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(JavaScriptEngine.java:596)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPossible(HtmlPage.java:997)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeEventListeners(EventListenersContainer.java:179)
at com.gargoylesoftware.htmlunit.javascript.host.EventListenersContainer.executeBubblingListeners(EventListenersContainer.java:239)
at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:827)
at com.gargoylesoftware.htmlunit.javascript.host.Node.fireEvent(Node.java:761)
at com.gargoylesoftware.htmlunit.html.HtmlElement$1.run(HtmlElement.java:917)
at net.sourceforge.htmlunit.corejs.javascript.Context.call(Context.java:620)
at net.sourceforge.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:513)
at com.gargoylesoftware.htmlunit.html.HtmlElement.fireEvent(HtmlElement.java:922)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeEventHandlersIfNeeded(HtmlPage.java:1294)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:252)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:462)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:329)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:394)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:379)
at WebRequest.main(WebRequest.java:23)
== CALLING JAVASCRIPT ==
function () {
c.removeEventListener("DOMContentLoaded", B, !1), e.ready();
}
======= EXCEPTION END ========