selenium如何識別驗證碼

一:前面的文章寫了如何右鍵另存爲圖片,把驗證碼存爲圖片後,接下來就是要作,怎麼把圖片上的內容獲取到,借住tesseract工具html

1.下載tesseract:http://sourceforge.net/projects/tesseract-ocr/java

2.安裝tesseract,安裝成功後,最好重啓電腦,由於eclipse要讀取path,在cmd輸入tesseract.exe,出現參數列表則安裝成功(不出現的話,就查看下系統path下是否有安裝路徑)web

3.將tesseract.exe命令保存爲bat文件,bat內容爲chrome

@echo off
tesseract.exe yzm.png 1 -l
exit

驗證碼圖片的位置最後放在項目的根目錄下apache

4.java調用該bat文件app

    String cmd = "cmd /c start d://yanzhengm.bat";
      try {
		Runtime.getRuntime().exec(cmd);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		
	} 

運行成功後,會生成一個1.txt文件,該文件保存了驗證碼的文本內容eclipse

5.java讀取文件得到文本內容ide

二:上面的方法是右鍵另存爲保存驗證碼圖片後,再識別圖片驗證碼,下面介紹用座標的方法保存驗證碼圖片工具

package com.imgyzm;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.FileHandler;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

/** 
 * @author QiaoJiaofei 
 * @version 建立時間:2015年8月27日 上午10:29:57 
 * 類說明 
 */
public class TestYzmByElementPoint {
    WebDriver dr;
    @BeforeTest
    public void before() {
        String key = "webdriver.chrome.driver";
        String value = "D:/BaiduYunDownload/selenium/chromedriver.exe";
        System.setProperty(key, value);
        dr = new ChromeDriver();
        dr.manage().window().maximize();
    }
    
    @Test
    public void test1() {
        dr.get("http://172.16.30.242:5555/register.shtml");
        WebDriverWait wait = new WebDriverWait(dr,10);
        WebElement element = wait.until(new ExpectedCondition<WebElement>() {

            @Override
            public WebElement apply(WebDriver arg0) {
                // TODO Auto-generated method stub
                return arg0.findElement(By.id("codeimg"));
            }
            
        });
        File scrFile = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);
        //WebElement element = dr.findElement(By.id("codeimg"));

        try {
            Point p = element.getLocation();
            int width = element.getSize().getWidth();
            int higth = element.getSize().getHeight();
            Rectangle rect = new Rectangle(width, higth);
            BufferedImage img = ImageIO.read(scrFile);
            BufferedImage dest = img.getSubimage(p.getX(), p.getY(), width, higth);
            ImageIO.write(dest, "png", scrFile);
            Thread.sleep(1000);
            File fng = new File("D:/ddd/yzm.png");
            if(fng.exists()){
                fng.delete();
            }
            FileUtils.copyFile(scrFile, fng);
            
            Runtime rt = Runtime.getRuntime();
            rt.exec("cmd.exe /C  tesseract.exe D:\\ddd\\yzm.png  D:\\ddd\\yzm -1 ");
            Thread.sleep(1000);
            File file = new File("D:\\ddd\\yzm.txt");
            if(file.exists()) {
                FileHandler fh = new FileHandler();
                String s = fh.readAsString(file).trim();
                System.out.println(s);
            } else {
                System.out.print("yzm.txt不存在");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
    }
    @AfterTest
    public void after() {
        dr.quit();
    }
}
相關文章
相關標籤/搜索