简体   繁体   中英

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.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 wich 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 aa huge refactoring regarding event handling.

As replacement you can use the enclosed page of the window.

Eg 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.

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 did the trick. I think I will stick with selenium except if there is some good reason not to?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM