编程语言
首页 > 编程语言> > 大屏25JAVA+selenium+tess4j识别登陆验证码截图与识别

大屏25JAVA+selenium+tess4j识别登陆验证码截图与识别

作者:互联网

大屏25JAVA+selenium+tess4j识别登陆验证码截图与识别

 

前面的文章写了如何右键另存为图片,把验证码存为图片后,接下来就是要作,怎么把图片上的内容获取到,借住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();
    }
}
  相关文章

 

做界面自动化时,很需要截图功能,譬如在异常发生时或者验证点失败时,这样可以快速的定位失败原因,但是如果使用界面截图的方式虽然会把这个屏幕截下来,但是缺点在于机器不能睡眠,如果睡下去则会发现截图是黑的;
其实我们可以使用selenium的截图功能,这种方式只会截取网站的部分(如顶部的浏览器输入框之类则不会截取),其优点在于在截图时操作电脑不会影响截图,未登录状态也受影响;
方法如下(driver为实例化webdriver):

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
1
而后再用commons-io包中的copyFile方法将其保存下来即可(savePath为文件保存路径);

FileUtils.copyFile(f, new File(savePath));
1
元素截图的方法为先全屏截图,然后根据元素坐标进行剪切,代码如下:

driver.get("http://www.google.com");
WebElement ele = driver.findElement(By.id("hplogo"));

// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);

// Get the location of element on the page
Point point = ele.getLocation();

// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();

// 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);

// Copy the element screenshot to disk
File screenshotLocation = new File("C:\\images\\GoogleLogo_screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);
————————————————
版权声明:本文为CSDN博主「df0128」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/df0128/article/details/80348729

 

标签:tess4j,openqa,selenium,验证码,org,import,识别
来源: https://www.cnblogs.com/xinxihua/p/14613735.html