package com.xxx;
import cn.hutool.core.io.file.FileWriter;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ImmediateRefreshHandler;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.ScriptResult;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.File;
import java.io.IOException;
public class JsTestMain {
public static void main(String[] args) throws IOException {
WebClient webclient = new WebClient(BrowserVersion.CHROME); // 设置浏览器版本
webclient.getOptions().setTimeout(600 * 1000);
webclient.getOptions().setRedirectEnabled(true);
webclient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webclient.getOptions().setUseInsecureSSL(true);
webclient.getOptions().setJavaScriptEnabled(true); // 启用javascript
webclient.getOptions().setThrowExceptionOnScriptError(false); // 关闭js的异常抛出
webclient.getOptions().setCssEnabled(false); // 不加载CSS文件
//webclient.getCookieManager().clearCookies();
//webclient.getCache().clear();
webclient.setJavaScriptTimeout(600 * 1000);
webclient.waitForBackgroundJavaScript(60 * 1000);
webclient.setAjaxController(new NicelyResynchronizingAjaxController());
webclient.setRefreshHandler(new ImmediateRefreshHandler());
String url = "https://www.baidu.com/";
HtmlPage page = webclient.getPage(url);
ScriptResult result = page.executeJavaScript("document.body.innerHTML = 'hello world';");
HtmlPage newPage = (HtmlPage)page.getWebClient().getCurrentWindow().getEnclosedPage();
String html = newPage.asXml();
FileWriter writer = new FileWriter("E:\\temp\\tmp\\logs" + File.separator + "aaa.txt");
writer.write(html);
}
}
以下代码可能只能在2.36前的版本使用:
final WebClient webClient=new WebClient();
webClient.getOptions().setCssEnabled(false);
HtmlPage page=webClient.getPage("目标地址");
String hrefValue = "js方法";
ScriptResult s = page.executeJavaScript(hrefValue);//执行js方法
page = (HtmlPage) s.getNewPage();//获得执行后的新page对象,新版本没这个方法了
System.out.println(page.asXml());
webClient.close();
参见: