htmlunit 自动化提交/获取网页数据,自动化测试

开源组件:

https://sourceforge.net/projects/htmlunit/

demo

public void post() {
        try {
            WebClient client = new WebClient(BrowserVersion.INTERNET_EXPLORER_7);
            client.setJavaScriptEnabled(false);
            HttpWebConnection httpwebconnection = new HttpWebConnection(client);
            // httpwebconnection.setUseInsecureSSL(true);
            ProxyConfig proxyConfig = new ProxyConfig();
            proxyConfig.setProxyHost("代理");
            proxyConfig.setProxyPort(8080);

            client.setProxyConfig(proxyConfig);
            client.setWebConnection(httpwebconnection);
            HtmlPage page = client.getPage("http://www.cmd5.com/default.aspx");

            HtmlElement textElemnt = page
                    .getElementById("ctl00_ContentPlaceHolder1_TextBoxInput");
            HtmlElement selectElement = page
                    .getElementById("ctl00_ContentPlaceHolder1_InputHashType");
            HtmlElement btnElement = page
                    .getElementById("ctl00_ContentPlaceHolder1_Button1");
            textElemnt.click();
            textElemnt.type("e10adc3949ba59abbe56e057f20f883e");
            selectElement.click();
            selectElement.type("MD5");
            // 点击事件,获取一个新的页面
            HtmlPage resultPage = (HtmlPage) btnElement.click();

            // 返回页面数据
            String result = resultPage.asText();
            // 截取字符串
            result = result.substring(result.indexOf("查询结果:") + 5).trim();
            int index = result.indexOf("[添加备注]");
            result = result.substring(0, index).trim();
            System.out.println(result);
        } catch (Exception ex) {

        }
    }

 自动化执行效果图

转载于:https://www.cnblogs.com/nidongde/p/5253726.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值