Example website: https://my.1hai.cn/Login/?url=http://www.1hai.cn/
First, the scene: For security reasons, more and more web platform login will increase the graphics verification Code (image), or SMS Verification code. Since is the picture script selenium is unrecognized, this is the time we parse the picture verification code.
Solution: 1. Navigate to the image by selenium and save the image locally.
2 Convert the image verification code into text using ORC technology.
Other workaround: A: Remove the verification code
B: Set the Universal code
Second, the implementation of Web image verification Code Source:
1 Packageutil;2 3 ImportJava.awt.Color;4 ImportJava.awt.Font;5 ImportJava.awt.Graphics;6 ImportJava.awt.image.BufferedImage;7 Importjava.io.IOException;8 ImportJava.io.OutputStream;9 ImportJava.util.Random;Ten ImportJavax.imageio.ImageIO; One A Public classMakecarke { - - //a character set that can appear in a captcha picture and can be modified as needed the Private CharMaptable[] = {' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', -' J ', ' K ', ' l ', ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', -' W ', ' x ', ' y ', ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', -' 9 ' }; + - /*** Function: Generate color verification Code picture * parameter width to generate the picture, height of the parameter, parameter OS for the output stream of the page*/ + PublicString Getcertpic (intWidthintheight, outputstream os) { A at if(Width <= 0) { -width = 60; - } - if(Height <= 0) { -Height = 20; - } inBufferedImage image =Newbufferedimage (width, height, - Bufferedimage.type_int_rgb); to //Get the graphics context +Graphics g =image.getgraphics (); - //Set Background color theG.setcolor (NewColor (0xDCDCDC)); *G.fillrect (0, 0, width, height); $ //Draw BorderPanax Notoginseng G.setcolor (color.black); -G.drawrect (0, 0, width-1, height-1); the //take the randomly generated authentication code +String strensure = ""; A //4 for 4-bit verification code, if you want to generate more bits of the authentication code, then increase the value the for(inti = 0; I < 4; ++i) { +Strensure + = maptable[(int) (Maptable.length *Math.random ())]; - } $ //Display the authentication code in the image, if you want to generate more bits of the authentication code, add drawstring statement $ G.setcolor (color.black); -G.setfont (NewFont ("Atlantic Inline", Font.plain, 18)); -String str = strensure.substring (0, 1); theg.DrawString (str, 8, 17); -str = strensure.substring (1, 2);Wuyig.DrawString (str, 20, 15); thestr = strensure.substring (2, 3); -g.DrawString (str, 35, 18); Wustr = strensure.substring (3, 4); -g.DrawString (str, 45, 15); About $ //randomly generates 10 interference points -Random Rand =NewRandom (); - for(inti = 0; I < 10; i++) { - intx =rand.nextint (width); A inty =rand.nextint (height); +G.drawoval (x, Y, 1, 1); the } - //releasing the graphics context $ g.dispose (); the Try { the //output image to page theImageio.write (Image, "JPEG", OS); the}Catch(IOException e) { - return""; in } the returnstrensure; the About } the the}
Third, Selenium+java implementation verification code picture save
1 PackageCom.app.launch;2 ImportJava.io.File; 3 ImportJava.awt.image.BufferedImage; 4 Importjava.io.IOException; 5 6 ImportJavax.imageio.ImageIO; 7 8 Importorg.apache.commons.io.FileUtils; 9 ImportOrg.openqa.selenium.By; Ten ImportOrg.openqa.selenium.OutputType; One ImportOrg.openqa.selenium.TakesScreenshot; A ImportOrg.openqa.selenium.WebDriver; - Importorg.openqa.selenium.WebElement; - ImportOrg.openqa.selenium.chrome.ChromeDriver; the - Importcom.app.utils.Utils; - - Public classOcrdownloadpicture { + - Public Static voidMain (string[] args)throwsIOException { + A Public voidDowanloadpictureverificationcode ()throwsioexception{ atDriver.get ("https://my.1hai.cn/Login/?url=http://www.1hai.cn/"); -Webelement ele = driver.findelement (By.xpath (".//img[@id = ' Quick_imgcaptcha ']")); - Ele.click (); -Utils.waitabit (2000); -File screenshot =((takesscreenshot) driver). Getscreenshotas (Outputtype.file); -BufferedImage fullimg =Imageio.read (screenshot); //Read in //Get The location of an element on the page -Org.openqa.selenium.Point point=ele.getlocation (); to //Get width and height of the element + intElewidth=ele.getsize (). GetWidth (); - inteleheight=ele.getsize (). GetHeight (); the //Crop the entire page screenshot to get only element screenshot *BufferedImage elescreenshot=Fullimg.getsubimage (Point.getx (), Point.gety (), Elewidth, eleheight); $Imageio.write (Elescreenshot, "PNG", screenshot); Panax Notoginseng //Copy The element screenshot to disk -File screenshotlocation =NewFile ("E:/vame/img/test.jpg"); the Fileutils.copyfile (screenshot, screenshotlocation); + } A the}
Four
Java How to get picture verification code Save