您的位置:首页 > 其它

selenium使用ocr识别实现网页动态验证码登录

2017-04-17 16:24 603 查看
算是自己实现的第一个有意义的程序吧,附ocr识别程序下载链接 :http://pan.baidu.com/s/1jHPMktg

目前只在fiefox上测试通过了,IE上面验证码一致都没有识别出来,以后再试试

package com.CAPTCHA.tests;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.concurrent.TimeUnit;

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.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.CAPTCHA.tests.*;

/**
* 登录需要验证码时,自动识别验证码
* @author fang
*
*/
public class TestCAPTCHALogin {

public static void main(String[] args) throws IOException,
InterruptedException {
WebDriver driver = new FirefoxDriver();
//testurl可以替换为你需要登录的网址
driver.get("testurl");
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
WebElement login = driver.findElement(By.linkText("登录"));
login.click();
WebElement element = driver.findElement(By.xpath(".//*[@id='validatecodeimg']"));
// take screen shot for element
screenShotForElement(driver, element, "D:\\Tesseract-OCR\\test.png");
// use Tesseract to get strings
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /C  tesseract.exe D:\\Tesseract-OCR\\test.png  D:\\Tesseract-OCR\\test -1 ");
Thread.sleep(1000);
// Read text
//readTextFile("D:\\Tesseract-OCR\\test.txt");
//String s =readTextFile("D:\\Tesseract-OCR\\test.txt");
System.out.println(readTextFile("D:\\Tesseract-OCR\\test.txt"));
String s = readTextFile("D:\\Tesseract-OCR\\test.txt");
//登录
//WebElement searchButton1 = driver.findElement(By.linkText("登录"));
//searchButton1.click();
//WebElement searchButton2 = driver.findElement(By.linkText("注册"));
//searchButton2.click();
WebElement searchButton3 = driver.findElement(By.id("userName"));
searchButton3.sendKeys("username");
WebElement searchButton4 = driver.findElement(By.id("userPass"));
searchButton4.sendKeys("123456");
WebElement searchButton5 = driver.findElement(By.xpath(".//*[@id='vadidatecode']"));
searchButton5.sendKeys(s);
WebElement login1 = driver.findElement(By.id("imageField"));
login1.click();
}

/**
* This method for read TXT file
*
* @param filePath
*/
public static String readTextFile(String filePath) {
try {
String encoding = "GBK";
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
// System.out.println(lineTxt);
//return lineTxt;
break;
}
read.close();
return lineTxt;
} else {
System.out.println("找不到指定的文件");
return null;
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
return null;
}
}
public static void screenShotForElement(WebDriver driver,
WebElement element, String path) throws InterruptedException {
File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
try {
Point p = element.getLocation();
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
Rectangle rect = new Rectangle(width, height);
BufferedImage img = ImageIO.read(scrFile);
BufferedImage dest = img.getSubimage(p.getX(), p.getY(),
rect.width, rect.height);
ImageIO.write(dest, "png", scrFile);
Thread.sleep(1000);
FileUtils.copyFile(scrFile, new File(path));
} catch (IOException e) {
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: