I'm trying to use Html Unit to run JavaScript on a webpage in order to change page.
I'm importing:
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.ScriptResult;
and the code is something like:
String javaScriptCode = "changePage(1)";
try{
webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setJavaScriptEnabled(true);
page = webClient.getPage(url);
newPage = page.executeJavaScript(javaScriptCode).getNewPage();
webClient.close();
} catch (Exception e) {
e.printStackTrace();
}
However I get the error:
[Java] The method getNewPage() is undefined for the type ScriptResult [67108964]
Which is crystal clear, however it doesn't tell me which method I'm supposed to use? Most of what I found on internet is based on the getNewPage method or on getPage but there may have been changes in the library...
see: calling a JavaScript function with HTMLUnit
I'm using:
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.33</version>
</dependency>
Yes you are right, the method getNewPage() was removed with the version 2.33 as part of a a huge refactoring regarding event handling.
As replacement you can use the enclosed page of the window.
E.g. if you are sure that the resulting page is in the same window as your current page (has replaced your page without opening a new window), you can use
page.getEnclosingWindow().getEnclosedPage()
If your code might opend up a new window it will be more save to ask the webClient for the current window (newly opened windows are automatically marked as the current)
page.getWebClient().getCurrentWindow().getEnclosedPage()
Or in your code sample you can directly use the client
webClient.getCurrentWindow().getEnclosedPage()
Hope that helps.