Skip to content Skip to sidebar Skip to footer

HtmlUnit How To Get A Page After Executing Javascript

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

Solution 1:

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.


Solution 2:

As for me, I gave a try to the selenium browser :

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>3.141.59</version>
</dependency>

And I find it very handful ! I just built a driver, a js executor, an the driver is up to date on the good page as soon as the JS is runned.

driver = new ChromeDriver();
js = (JavascriptExecutor)driver;
driver.get(url);
js.executeScript(script);

Four lines id the trick. I think I will stick with selenium except if there is some good reason not to?


Post a Comment for "HtmlUnit How To Get A Page After Executing Javascript"