Htmlunit Check Verification Code
Directly on the code
1String url = "http://www.zycg.gov.cn/";2 3Webclientutil webclientutils =Newwebclientutil ();4WebClient WebClient =webclientutils.getwebclient ();5 6HtmlPage page =webclient.getpage (URL);7 8HtmlElement username = Page.getfirstbyxpath ("//*[@id = ' u_name ']");9HtmlElement password = Page.getfirstbyxpath ("//*[@id = ' u_pwd1 ']");TenHtmlElement Valicode = Page.getfirstbyxpath ("//*[@id = ' Yzm ']"); OneHtmlImage valicodeimg = (htmlimage) page.getfirstbyxpath ("//*[@id = ' img_random ']"); AImageReader ImageReader =Valicodeimg.getimagereader (); -BufferedImage bufferedimage = imagereader.read (0); - theJFrame F2 =NewJFrame (); -JLabel L =NewJLabel (); -L.seticon (NewImageIcon (BufferedImage)); - F2.getcontentpane (). Add (l); +F2.setsize (100, 100); -F2.settitle ("Verification Code")); +F2.setvisible (true); A atString valicodestr = Joptionpane.showinputdialog ("Please enter the CAPTCHA:"); -F2.setvisible (false); -HtmlForm submit = Page.getfirstbyxpath ("//*[@id = ' submitForm ']"); - - Username.click (); -Username.type ("Zhongkeruan"); in Password.click (); -Password.type ("62570007"); to Valicode.click (); + Valicode.type (VALICODESTR); - the *Scriptresult result = Page.executejavascript ("DL ()"); $page =(HtmlPage) result.getnewpage ();Panax Notoginseng - if(Page.astext (). Contains ("Next page")){ theSYSTEM.OUT.PRINTLN ("Login Successful"); +}Else{ ASYSTEM.OUT.PRINTLN ("Login Failed"); the } +System.out.println (Page.astext ());
Htmlunit Check Verification Code