HtmlUnit How to get a page after executing Javascript

萝らか妹 提交于 2020-01-25 12:15:27

问题


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>

回答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.




回答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?



来源:https://stackoverflow.com/questions/53795492/htmlunit-how-to-get-a-page-after-executing-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!