0

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();
Enigmatic
  • 3,902
  • 6
  • 26
  • 48
  • See this [post](https://stackoverflow.com/questions/53795492/htmlunit-how-to-get-a-page-after-executing-javascript). Seems like it was removed in later versions. – Nexevis Sep 23 '19 at 14:27
  • @Nexevis Thanks, although this doesn't solve the error. It now says `getCurrentWindow() is undefined for type ScriptResult`. – Enigmatic Sep 23 '19 at 14:34
  • It would be useful to provide the version of `HTMLUnit` you are using, as this seems to be an issue that depends on your version. – Nexevis Sep 23 '19 at 14:35
  • @Nexevis Apologies, 2.36 – Enigmatic Sep 23 '19 at 14:36
  • 1
    Perhaps `.getJavaScriptResult()` is what you are looking for? I don't see many other options in the [docs](http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/ScriptResult.html#getJavaScriptResult--) for that version. – Nexevis Sep 23 '19 at 14:43

1 Answers1

1

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.

  • Thanks :) Pressing the button opens a new page, I will then be required to press another JS button which would load once again another new page. How does this allow me to do so? From what I understand this simply outputs the result as opposed the setting a new HtmlPage. EDIT: I would like the ability to progress onto the next page once which occurs once the button is pressed. – Enigmatic Sep 23 '19 at 16:40
  • I've posted a more specific question relating to my query: https://stackoverflow.com/questions/58066950/navigating-through-a-website-by-pressing-on-js-buttons-using-htmlunit – Enigmatic Sep 23 '19 at 16:53