Search code examples
javaposthtmlunit

The method GetNewPage() is undefined for type ScriptResult


I'm attempting to press a Javascript Button on a webpage using HTMLUnit 2.36 to progress onto the next page:

ScriptResult result = page.executeJavaScript("__doPostBack('LinkBtn_thebutton','')");
Page page = result.getNewPage();

I've attempted to use the code above which causes the following error:

The method GetNewPage() is undefined for type ScriptResult

EDIT:

I've also attempted the following with no luck:

HtmlPage page1 = (HtmlPage) result.getJavaScriptResult();

Solution

  • The proper way to execute javascirpt is as follows:

    String javaScriptCode = "";
    ScriptResult result = page.executeJavaScript(javaScriptCode);
    result.getJavaScriptResult();
    

    Dont force a refresh of the page, it will be handled of the ScriptResult.

    You should also keep in mind that HtmlUnit usually comes with bugs regarding Javascript. Try to switch between the BrowserVersion if you should encounter weird js behavior.