selenium使用ocr识别实现网页动态验证码登录
来源:互联网 发布:4gip网络加速器下载 编辑:程序博客网 时间:2024/06/06 06:39
算是自己实现的第一个有意义的程序吧,附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(); } } }
0 0
- selenium使用ocr识别实现网页动态验证码登录
- win7 selenium python 验证码识别 pytesser使用 安装Pillow、pytesser、tesseract-ocr
- python+selenium识别验证码并登录
- python+selenium识别验证码并登录
- OCR验证码识别
- ocr识别验证码
- 使用Tesseract-ocr识别验证码,暴力破解某高校学生登录系统
- 使用(ImageMagick+tesseract-ocr)实现图像验证码识别实例
- 使用(ImageMagick+tesseract-ocr)实现图像验证码识别实例
- 无忧OCR验证码识别
- java 验证码识别 ocr
- 通过OCR实现对简单验证码的识别技术
- 利用tessnet2_32.dll实现OCR验证码识别
- 利用tessnet2_32.dll实现OCR验证码识别
- python使用tesseract-ocr完成验证码识别
- python 使用tesseract-ocr , pytesseract , PIL进行验证码识别
- python使用tesseract-ocr完成验证码识别
- python使用tesseract-ocr完成验证码识别
- rsync同步的一些用法
- js 设置读秒验证码
- centOS服务器 netstat命令 查看TCP连接数信息
- iOS 获取权限描述本地化
- 11g to 12c Upgrade/Migration Using Full Transportable TableSpace & Data Pump
- selenium使用ocr识别实现网页动态验证码登录
- WebSocket(叁) 生成数据帧
- JavaScript之Function和function简单区分
- 大型电商类网站架构图
- 洛谷 P3708 koishi的数学题
- Android设备信息IP地址等获取
- 国科大软件安全与漏洞分析第一次作业
- Input 标签使用和属性
- C++中文件流和文件的区分