使用 HTMLUnit 调用 JavaScript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21024176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling a JavaScript function with HTMLUnit
提问by Der Fede
I'm trying to call the function showPage('3');
of this page, for use the page source code after. I tried to do with htmlUnit
like so:
我想调用的函数showPage('3');
的这个页面,为以后使用页面的源代码。我试图htmlUnit
这样做:
WebClient webClient = new WebClient();
webClient.waitForBackgroundJavaScriptStartingBefore(10000);
HtmlPage page = webClient.getPage("http://www.visittrentino.it/it/cosa_fare/eventi/risultati?minEventDate=09012014&maxEventDate=31012014&tp=searchForm.thismonth<p=gennaio");
String javaScriptCode = "showPage('3');";
ScriptResult result = page.executeJavaScript(javaScriptCode);
result.getJavaScriptResult();
System.out.println("result: "+ result);
But it's not working. It prints out:
但它不起作用。它打印出:
result: net.sourceforge.htmlunit.corejs.javascript.Undefined@a303147
结果:net.sourceforge.htmlunit.corejs.javascript.Undefined@a303147
and other 10000 warnings. What am I doing wrong? I need to change the page of this site for do some crawling on the source code. Is there another way (and maybe more easier) for calling jsp-function from Java code and then navigate in the source of the page? Thank you for any help, have a nice day.
和其他 10000 个警告。我究竟做错了什么?我需要更改此站点的页面以对源代码进行一些抓取。是否有另一种方法(也许更简单)从 Java 代码调用 jsp-function 然后在页面的源代码中导航?感谢您的任何帮助,祝您有美好的一天。
采纳答案by Kick
You print the ScriptResult object not the content of the page,change the SOP code to result.getNewPage()
您打印的是 ScriptResult 对象而不是页面的内容,将 SOP 代码更改为 result.getNewPage()