使用 HTMLUnit 通过按下 JS 按钮来浏览网站

Navigating through a website by pressing on JS buttons using HTMLUnit

我正在尝试使用 HTMLUnit 2.36 在网页上按 Javascript 按钮导航到另一个页面,依此类推...:

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

我尝试使用上面的代码导致以下错误,据推测是因为不再支持 getNewPage()

The method GetNewPage() is undefined for type ScriptResult

我也曾尝试使用 getJavaScriptResult() 添加演员表,如下所示,但没有成功:

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

导致以下错误:

Exception in thread "main" java.lang.ClassCastException: class net.sourceforge.htmlunit.corejs.javascript.Undefined cannot be cast to class com.gargoylesoftware.htmlunit.html.HtmlPage

您不应该施放 result.getJavaScriptResult();,将其视为无效。如果要重定向您的页面,请确保已启用重定向:webClient.getOptions().setRedirectEnabled(true);