7

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Der Fede
  • 73
  • 1
  • 1
  • 5

1 Answers1

7

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

Kick
  • 4,823
  • 3
  • 22
  • 29
  • welcome and if the answer work for u then select it as correct answer – Kick Jan 10 '14 at 17:32
  • Object result = page.executeJavaScript(javaScriptCode).getJavaScriptResult(); page.getPage(); System.out.println("result: "+result+"\n"+page.getPage()); result is a object and dont have .getNewPage() or something so method... – Der Fede Jan 20 '14 at 16:24
  • ScriptResult result = page.executeJavaScript(javaScriptCode); then you can print by using result.getNewPage(). – Kick Jan 21 '14 at 14:00
  • sry man, now i understand you! ty for the help have a nice day! – Der Fede Jan 22 '14 at 15:44
  • 4
    `ScriptResult.getNewPage()` was removed in a recent version. The change suggests to use `page.getWebClient().getCurrentWindow().getEnclosedPage()` instead: https://github.com/HtmlUnit/htmlunit/commit/120d9040184cecea08073ccacb3383848a7f9746#diff-32b86263e0cea174da9b8ceb6f9482fc – Martin Prebio Dec 27 '18 at 19:14