zoukankan      html  css  js  c++  java
  • 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) {
    
            }
        }

     自动化执行效果图

  • 相关阅读:
    codeforce 148D. Bag of mice[概率dp]
    poj2096 Collecting Bugs[期望dp]
    poj3744 Scout YYF I[概率dp+矩阵优化]
    hdu 5318 The Goddess Of The Moon
    hdu5411 CRB and Puzzle[矩阵优化dp]
    poj3734 Blocks[矩阵优化dp or 组合数学]
    1948 NOI 嘉年华
    教主泡嫦娥[有趣的dp状态设计]
    HDU 4455 Substrings[多重dp]
    1296: [SCOI2009]粉刷匠[多重dp]
  • 原文地址:https://www.cnblogs.com/nidongde/p/5253726.html
Copyright © 2011-2022 走看看