HtmlUnit 执行自定义JS,并获取执行后的结果,版本2.42.0

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();

参见:

https://sourceforge.net/p/htmlunit/bugs/2023/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值