使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 19:51:32  来源:igfitidea点击:

calling a JavaScript function with HTMLUnit

javascripthtmlunit

提问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 htmlUnitlike 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&ltp=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()