htmlunit 模擬登陸 數字驗證碼

使用htmlunit的好處有兩點,相比httpclient,htmlunit是對瀏覽器的模擬,好比你定位一個按鈕,就能夠執行click()方法,此外不須要象在httpclient中同樣編寫複雜的代碼,如一堆request header還有一大堆請求參數,你只須要填寫用戶名,密碼,驗證碼便可,就象在使用一個沒有界面的瀏覽器,固然更重要的是htmlunit對js的支持設置極其簡單javascript

1.添加maven的htmlunit(標紅)依賴html

 1  <dependencies>
 2     <dependency>
 3         <groupId>junit</groupId>
 4         <artifactId>junit</artifactId>
 5         <version>${junit.version}</version>
 6         <scope>test</scope>
 7     </dependency>
 8     <dependency>
 9         <groupId>com.alibaba</groupId>
10         <artifactId>fastjson</artifactId>
11         <version>1.2.47</version>
12     </dependency>
13     <dependency>
14         <groupId>org.jsoup</groupId>
15         <artifactId>jsoup</artifactId>
16         <version>1.11.3</version>
17     </dependency>
18     <dependency> 19 <groupId>net.sourceforge.htmlunit</groupId> 20 <artifactId>htmlunit</artifactId> 21 <version>2.18</version> 22 </dependency>
23     
24   </dependencies>

 

2.思路java

定位用戶名,密碼,驗證碼框等元素,填寫便可,驗證碼能夠先把圖片下載下來而後手動輸入,也能夠使用tess4j進行圖片識別,這裏是手動輸入測試的網站,是一個僞ajaxsubmit,測試屢次發現須要二次輸入驗證碼才能正確登陸,但使人疑惑的是兩次生成的驗證碼同樣(若是不同說明你第一次輸入錯誤)web

 

 1 public static void main(String[] args) throws Exception {  2         WebClient webClient = new WebClient(BrowserVersion.CHROME);  3         webClient.getOptions().setJavaScriptEnabled(true);  4         webClient.getOptions().setCssEnabled(true);  5         webClient.getOptions().setThrowExceptionOnScriptError(false);  6     // webClient.getOptions().setThrowExceptionOnFailingStatusCode(true);
 7         webClient.getOptions().setActiveXNative(false);  8         
 9         
10         //ajax
11         webClient.setAjaxController(new NicelyResynchronizingAjaxController()); 12         webClient.getOptions().setUseInsecureSSL(false); 13         
14         
15         //容許重定向
16         webClient.getOptions().setRedirectEnabled(true); 17                 
18         
19         //鏈接超時
20         webClient.getOptions().setTimeout(5000); 21         
22         //js執行超時
23         webClient.setJavaScriptTimeout(10000*3); 24         
25         //對於此網站務必開啓
26         webClient.getCookieManager().setCookiesEnabled(true); 27         
28         String url = "https://www.zuhaowan.com/login/"; 29         HtmlPage page = webClient.getPage(url); 30         webClient.waitForBackgroundJavaScript(5000); 31         
32         
33         
34 
35         HtmlPage newPage = readyPage(page, webClient); 36     // String content1 = newPage.asXml(); 37     // IOUtils.write(content1.getBytes(),new FileWriter(new File("f:/content1.txt"))); 38         
39         //若是頁面url沒有變化從新進行一次登陸
40         if(newPage.getUrl().toString().equals(url)) { 41             System.out.println("出現錯誤請從新登陸-------------"); 42             HtmlPage result = readyPage(newPage,webClient); 43             System.out.println("url----------------"+result.getUrl()); 44             System.out.println("頁面----" + result.asXml()); 45         // IOUtils.write(result.asXml(),new FileWriter(new File("f:/content2.txt")));
46             
47  } 48         
49  webClient.close(); 50  } 51     
52     
53     
54     public static HtmlPage readyPage(HtmlPage page,WebClient webClient) throws Exception { 55         //封裝頁面元素
56         HtmlForm form  = page.getHtmlElementById("form2"); 57         HtmlTextInput loginname = form.getInputByName("loginname"); 58         loginname.setValueAttribute("用戶名"); 59         HtmlPasswordInput loginpwd = form.getInputByName("loginpwd"); 60         loginpwd.setValueAttribute("密碼"); 61         
62         //驗證碼輸入框
63         HtmlTextInput verify_code = form.getInputByName("verify_code"); 64         
65         //驗證碼圖片
66         HtmlImage verify_img = (HtmlImage) page.getElementById("verify_img"); 67         UUID randomUUID = UUID.randomUUID(); 68         //保存
69         verify_img.saveAs(new File("./src/main/resources/image/verifyimg"+ randomUUID.toString() +".png")); 70         
71         System.out.println("驗證碼圖片已保存!"); 72         System.out.println("請輸入驗證碼"); 73         //手動輸入驗證碼
74         Scanner scanner = new Scanner(System.in); 75         String code = scanner.nextLine(); 76         System.out.println("驗證碼-------------" + code); 77  verify_code.setValueAttribute(code); 78         
79     
80         //登陸按鈕也能夠使用page.executeJavaScript("javascript:document.getElementById('loginsubmit').click()").getNewPage();
81         HtmlAnchor login = page.getHtmlElementById("loginsubmit"); 82         HtmlPage newPage = login.click(); 83         
84         //等待js加載
85         webClient.waitForBackgroundJavaScript(5000); 86         return newPage; 87             
88  } 89     

 

3.控制檯部分輸出截圖ajax

相關文章
相關標籤/搜索