calling a JavaScript function with HTMLUnit

Der Fede picture Der Fede · Jan 9, 2014 · Viewed 16.4k times · Source

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:

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

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.

Answer

Kick picture Kick · Jan 9, 2014

You print the ScriptResult object not the content of the page,change the SOP code to result.getNewPage()