htmlunit 点击复选框进行选择和模拟点击

时间:2024-04-02 20:28:46

package a.test;

import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

/**
 * 
 * @author Administrator
 *
 */
public class HkltCrawlerImpl{
    
     public static void main(String[] args) {
         try {
            start("350582199001122011");//身份证id
        } catch (FailingHttpStatusCodeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
     public static String start(String id) throws FailingHttpStatusCodeException, MalformedURLException, IOException {  final WebClient webClient = new WebClient();
     
     HtmlPage page1 = webClient.getPage("http://218.85.65.43:28000/FJWeb/Web/Studytimeselect.aspx");//获取要爬的页面地址
     HtmlSelect select = (HtmlSelect) page1.getElementByName("drop_cx");//获取复选框
     HtmlOption option = select.getOptionByValue("B1");//选择复选框里的选项
     HtmlPage page3 = option.click();//模拟点击
     
     HtmlInput input1 = page1.getElementByName("txt_sfzh_search");//获取一个输入框
     input1.setValueAttribute(id);//输入框里输入内容

     HtmlSubmitInput submit1 = page1.getElementByName("btn_search");//获取查询按钮
     page1=submit1.click();//模拟点击
     
     System.out.println(page1.asXml()); //输出xml代码页面的形式
     HtmlElement pxdw = page1.getHtmlElementById("lbl_yhmc");//获取xml代码页面中培训单位
     HtmlElement xm = page1.getHtmlElementById("lbl_xm");//获取xml代码页面中学员姓名
     HtmlElement time = page1.getHtmlElementById("lbl_bmrq");//获取xml代码页面报名时间
     String a = pxdw.getTextContent();//获取中培训单位
     String b = xm.getTextContent();//获取学员姓名
     String c = time.getTextContent();//获取报名时间
     String html=a+" "+b+" "+c;
     System.out.println(html);
     webClient.closeAllWindows();//关掉
     return html;
     }

htmlunit 点击复选框进行选择和模拟点击