HtmlUnit 使用范例

本文介绍了如何使用HtmlUnit库进行网页模拟操作,包括设置JavaScript支持、处理SSL证书问题和进行网页元素交互。示例代码展示了登录过程,并列举了项目所依赖的各个库。
摘要由CSDN通过智能技术生成

可以结合 Jsonp 解析

官方网站:http://htmlunit.sourceforge.net/dependencies.html

下载地址:https://sourceforge.net/projects/htmlunit/files/htmlunit/

 

/**
 * If set to <code>true</code>, the client will accept connections to any host, regardless of
 * whether they have valid certificates or not. This is especially useful when you are trying to
 * connect to a server with expired or corrupt certificates.
 * @param useInsecureSSL whether or not to use insecure SSL
 */
public void setUseInsecureSSL(final boolean useInsecureSSL) {
    useInsecureSSL_ = useInsecureSSL;
}

 

所以在init()时,也就是构造一个webclient时 ,打开最后的注释行即可解决这个异常……

public void init() throws Exception {
        webclient = new WebClient(BrowserVersion.FIREFOX_17);
        webclient.getOptions().setJavaScriptEnabled(true);
        webclient.getOptions().setThrowExceptionOnScriptError(false);
        webclient.getOptions().setCssEnabled(false);
        webclient.getCookieManager().clearCookies();
        webclient.getCache().clear();
        webclient.setRefreshHandler(new ImmediateRefreshHandler());
        webclient.getOptions().setTimeout(600*1000);
        webclient.setJavaScriptTimeout(600*1000);   
        webclient.setAjaxController(new NicelyResynchronizingAjaxController());  
        webclient.getOptions().setJavaScriptEnabled(true);   
        webclient.setJavaScriptTimeout(600*1000);   
        webclient.getOptions().setRedirectEnabled(true);
        webclient.waitForBackgroundJavaScript(60*1000);
        webclient.getOptions().setThrowExceptionOnScriptError(false);   
        webclient.getOptions().setThrowExceptionOnFailingStatusCode(false);
//        w

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTMLUnit是一个Java库,它用于模拟浏览器行为并执行Web页面的自动化测试。下面是一个简单的HTMLUnit使用教程: 1. 首先,你需要在你的Java项目中导入HTMLUnit库。你可以通过在你的构建工具(如Maven或Gradle)中添加以下依赖来实现: ```xml <dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId> <version>2.53.0</version> </dependency> ``` 2. 创建一个新的Java类,并导入HTMLUnit相关的类和方法: ```java import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; ``` 3. 在你的代码中创建一个WebClient对象,并启用JavaScript支持(如果需要执行JavaScript): ```java WebClient webClient = new WebClient(); webClient.getOptions().setJavaScriptEnabled(true); ``` 4. 使用WebClient对象获取HtmlPage对象,该对象表示要测试的Web页面: ```java HtmlPage page = webClient.getPage("http://example.com"); ``` 5. 一旦你有了HtmlPage对象,你就可以通过使用HTML DOM模型中的方法来获取页面的元素或执行其他操作。例如,你可以通过元素的ID获取元素: ```java HtmlElement element = page.getElementById("myElementId"); ``` 6. 你还可以模拟用户与页面的交互,例如填写表单、点击按钮等。例如,要填写一个文本框,你可以使用以下代码: ```java HtmlTextInput textField = (HtmlTextInput) page.getElementById("myTextFieldId"); textField.setValueAttribute("Hello, HTMLUnit!"); ``` 7. 最后,记得在使用完毕后关闭WebClient对象,以释放资源: ```java webClient.close(); ``` 这只是一个HTMLUnit的简单使用教程,你可以根据你的具体需求进一步深入学习和了解HTMLUnit的功能和特性。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值