(java)

http://www.bubuko.com/infodetail-1007293.html

[登录]  [注册]

布布扣,bubuko.com
首页 > Web开发 > 详细
id="cproIframe_u2385951_1" width="680" height="250" src="http://pos.baidu.com/acom?adn=0&adp=1&at=0&aurl=&c01=1&cad=1&ccd=24&cec=UTF-8&cfv=0&ch=0&col=zh-CN&conBW=1&conOP=1&cpa=1&cpro_lu=1%2C%23dfe4f9%2C%23000000%2C%E5%AE%8B%E4%BD%93&dai=1&dis=0&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DGwyMa3rNDW5ZzeoO1fOeBxTBGL0lHzGevZEspmulpZfetnKRvhocuLqlDNxPyeN0UNRcd_cDvQgtUT4PQlTuoK%26wd%3D%26eqid%3Ddfdcafe100011a2600000005563bac6a&ltu=http%3A%2F%2Fwww.bubuko.com%2Finfodetail-1007293.html&lu_161=0&lunum=6&n=65035100_cpr&pat=6&pcs=1280x675&pih=0&pis=10000x10000&piw=0&ps=152x140&psr=1280x800&pss=1280x675&ptbg=90&ptp=0&ptt=0&qn=c5a2d2ba6879747c&rad=&rsi0=680&rsi1=250&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%23000000&rss3=&rss4=&rss5=&rss6=%23e10900&rss7=&scale=&skin=tabcloud_skin_5&stid=5&td_id=2385951&titFF=%25E5%25BE%25AE%25E8%25BD%25AF%25E9%259B%2585%25E9%25BB%2591&titFS=14&titSU=0&titTA=left&tn=baiduCustNativeAD&tpr=1446750794660&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u2385951&ti=Webdriver%E9%85%8D%E5%90%88Tesseract-OCR%20%E8%87%AA%E5%8A%A8%E8%AF%86%E5%88%AB%E7%AE%80%E5%8D%95%E7%9A%84%E9%AA%8C%E8%AF%81%E7%A0%81&tt=1446750794620.42.233.238" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="padding: 0px;">

Webdriver配合Tesseract-OCR 自动识别简单的验证码

时间:2015-07-31 01:07:13      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:des   class   style   log   com   http   使用   代码   src


验证码: 如下,在进行自动化测试,遇到验证码的问题,一般有两种方式

技术分享

1.找开发去掉验证码或者使用万能验证码

2.使用OCR自动识别


使用OCR自动化识别,一般识别率不是太高,处理一般简单验证码还是没问题

这里使用的是Tesseract-OCR,下载地址:https://github.com/A9T9/Free-Ocr-Windows-Desktop/releases

怎么使用呢?

进入安装后的目录:

tesseract.exe test.png test -1 


准备一份网页,上面使用该验证码

<html>
<head>
<title>Table test by Young</title>
</head>
<body></br>
<h1> Test </h1><imgsrc="http://csujwc.its.csu.edu.cn/sys/ValidateCode.aspx?t=1"></br>
</body>
</html>

识别验证码,首先得取得验证码,这两款采取对 页面元素部分截图的方式,首先获取整个页面的截图

然后找到页面元素坐标进行截取

/*** This method for screen shot element* *@paramdriver*@paramelement*@parampath*@throwsInterruptedException*/public static voidscreenShotForElement(WebDriver driver,WebElement element, String path)throwsInterruptedException {File scrFile=((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);try{Point p=element.getLocation();int width =element.getSize().getWidth();int height =element.getSize().getHeight();Rectangle rect= newRectangle(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,newFile(path));}catch(IOException e) {e.printStackTrace();}}


截取完元素,就可以调用Tesseract-OCR生成text

//use Tesseract to get stringsRuntime rt =Runtime.getRuntime();rt.exec("cmd.exe /C  tesseract.exe D:\\Tesseract-OCR\\test.png  D:\\Tesseract-OCR\\test -1 ");

接下来通过java读取txt

/*** This method for read TXT file* *@paramfilePath*/public static voidreadTextFile(String filePath) {try{String encoding= "GBK";File file= newFile(filePath);if (file.isFile() && file.exists()) { //判断文件是否存在InputStreamReader read = newInputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式BufferedReader bufferedReader = newBufferedReader(read);String lineTxt= null;while ((lineTxt = bufferedReader.readLine()) != null) {System.out.println(lineTxt);}read.close();}else{System.out.println("找不到指定的文件");}}catch(Exception e) {System.out.println("读取文件内容出错");e.printStackTrace();}}


整体代码如下:

1 packagecom.dbyl.tests;2
3 importjava.awt.Rectangle;4 importjava.awt.image.BufferedImage;5 importjava.io.BufferedReader;6 importjava.io.File;7 importjava.io.FileInputStream;8 importjava.io.IOException;9 importjava.io.InputStreamReader;10 importjava.io.Reader;11 importjava.util.concurrent.TimeUnit;12
13 importjavax.imageio.ImageIO;14
15 importorg.apache.commons.io.FileUtils;16 importorg.openqa.selenium.By;17 importorg.openqa.selenium.OutputType;18 importorg.openqa.selenium.Point;19 importorg.openqa.selenium.TakesScreenshot;20 importorg.openqa.selenium.WebDriver;21 importorg.openqa.selenium.WebElement;22
23 importcom.dbyl.libarary.utils.DriverFactory;24
25 public classTesseractTest {26
27     public static void main(String[] args) throwsIOException,28 InterruptedException {29
30         WebDriver driver =DriverFactory.getChromeDriver();31         driver.get("file:///C:/Users/validation.html");32         driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);33         WebElement element = driver.findElement(By.xpath("//img"));34
35         //take screen shot for element
36         screenShotForElement(driver, element, "D:\\Tesseract-OCR\\test.png");37
38 driver.quit();39
40         //use Tesseract to get strings
41         Runtime rt =Runtime.getRuntime();42         rt.exec("cmd.exe /C  tesseract.exe D:\\Tesseract-OCR\\test.png  D:\\Tesseract-OCR\\test -1 ");43
44         Thread.sleep(1000);45         //Read text
46         readTextFile("D:\\Tesseract-OCR\\test.txt");47 }48
49     /**
50 * This method for read TXT file51 *52 *@paramfilePath53      */
54     public static voidreadTextFile(String filePath) {55         try{56             String encoding = "GBK";57             File file = newFile(filePath);58             if (file.isFile() && file.exists()) { //判断文件是否存在
59                 InputStreamReader read = newInputStreamReader(60                         new FileInputStream(file), encoding);//考虑到编码格式
61                 BufferedReader bufferedReader = newBufferedReader(read);62                 String lineTxt = null;63                 while ((lineTxt = bufferedReader.readLine()) != null) {64 System.out.println(lineTxt);65 }66 read.close();67             } else{68                 System.out.println("找不到指定的文件");69 }70         } catch(Exception e) {71             System.out.println("读取文件内容出错");72 e.printStackTrace();73 }74 }75
76     /**
77 * This method for screen shot element78 *79 *@paramdriver80 *@paramelement81 *@parampath82 *@throwsInterruptedException83      */
84     public static voidscreenShotForElement(WebDriver driver,85             WebElement element, String path) throwsInterruptedException {86         File scrFile =((TakesScreenshot) driver)87 .getScreenshotAs(OutputType.FILE);88         try{89             Point p =element.getLocation();90             int width =element.getSize().getWidth();91             int height =element.getSize().getHeight();92             Rectangle rect = newRectangle(width, height);93             BufferedImage img =ImageIO.read(scrFile);94             BufferedImage dest =img.getSubimage(p.getX(), p.getY(),95 rect.width, rect.height);96             ImageIO.write(dest, "png", scrFile);97             Thread.sleep(1000);98             FileUtils.copyFile(scrFile, newFile(path));99         } catch(IOException e) {100 e.printStackTrace();101 }102 }103
104 }

View Code

Webdriver配合Tesseract-OCR 自动识别简单的验证码

标签:des   class   style   log   com   http   使用   代码   src

(0)

(0)

   

举报
width="336" height="280" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_0" name="aswift_0" style="padding: 0px; left: 0px; position: absolute; top: 0px;">
width="336" height="280" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_1" name="aswift_1" style="padding: 0px; left: 0px; position: absolute; top: 0px;">

评论一句话评论(0
0

登录后才能评论! 

width="300" height="600" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_2" name="aswift_2" style="padding: 0px; left: 0px; position: absolute; top: 0px;">
分享档案
更多>

2015年11月06日 (207)

2015年11月05日 (2152)

2015年11月04日 (1852)

2015年11月03日 (1679)

2015年11月02日 (1920)

2015年11月01日 (1271)

2015年10月31日 (1325)

2015年10月30日 (1667)

2015年10月29日 (2108)

2015年10月28日 (1966)

文章周排行
更多>

  • 45种攻入网站后台的方法  2014-09-21
  • chrome jsonView插件安装  2015-03-26
  • 将Web项目War包部署到Tomcat服务器基本步骤  2015-01-17
  • dubbox开发rest+json指南【转】  2014-11-23
  • HTML中的<select>标签如何设置默认选中的选项  2014-11-12
  • 论文笔记:Faster R-CNN:Towards Real-Time Object Detection with Region Proposal Networks  2015-06-17
  • CSS实现响应式全屏背景图  2015-03-14
  • WebStorm+Node.js开发环境的配置  2015-03-13
  • CSS属性  2015-03-14
  • 312个免费高速HTTP代理IP(能隐藏自己真实IP地址)  2015-04-14

最新新闻
更多

  • 揭秘谷歌AMP项目:它是如何提高网页加载速度的?  2015-11-05
  • 百度通过跟踪用户发现鬼城  2015-11-05
  • 国产大飞机C919换上航空公司涂装效果图  2015-11-05
  • 入室小偷竟替户主代签收快递 案发后被快递员指认  2015-11-05
  • 三季度空气净化器品牌口碑排行榜  2015-11-05
  • BBC商店上线 可付费下载数字内容  2015-11-05
  • 苹果新专利:指纹进入应急模式,iPhone 可能救你一命  2015-11-05
  • 研究显示Android和iOS应用都会大量收集用户数据  2015-11-05
  • 北京移动VoLTE试商启动 推4G高清语音业务  2015-11-05
  • 保险业规定使用国内科技产品引国外团体不满  2015-11-05

width="300" height="250" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_3" name="aswift_3" style="padding: 0px; left: 0px; position: absolute; top: 0px;">
id="cproIframe_u2385978" src="http://pos.baidu.com/acom?adn=3&adp=1&at=0&aurl=&c01=1&cad=1&ccd=24&cec=UTF-8&cfv=0&ch=0&col=zh-CN&conBW=1&conOP=0&cpa=1&dai=2&dis=0&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DGwyMa3rNDW5ZzeoO1fOeBxTBGL0lHzGevZEspmulpZfetnKRvhocuLqlDNxPyeN0UNRcd_cDvQgtUT4PQlTuoK%26wd%3D%26eqid%3Ddfdcafe100011a2600000005563bac6a&ltu=http%3A%2F%2Fwww.bubuko.com%2Finfodetail-1007293.html&lunum=6&n=65035100_cpr&pat=17&pcs=1263x675&pis=10000x10000&ps=2504x831&psr=1280x800&pss=1263x8311&ptt=0&qn=df2cd68e9a9a5524&rad=&rsi0=300&rsi1=300&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%23000000&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=&td_id=2385978&titFF=%25E5%25BE%25AE%25E8%25BD%25AF%25E9%259B%2585%25E9%25BB%2591&titFS=14&titSU=0&tn=baiduCustNativeAD&tpr=1446750794660&ts=1&xuanting=1&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u2385978&ti=Webdriver%E9%85%8D%E5%90%88Tesseract-OCR%20%E8%87%AA%E5%8A%A8%E8%AF%86%E5%88%AB%E7%AE%80%E5%8D%95%E7%9A%84%E9%AA%8C%E8%AF%81%E7%A0%81&tt=1446750794620.1746.1951.1954" width="300" height="300" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="padding: 0px;">

id="cproIframe_u1738566_3" width="680" height="250" src="http://pos.baidu.com/acom?adn=4&adp=1&at=0&aurl=&c01=1&cad=1&ccd=24&cec=UTF-8&cfv=0&ch=0&col=zh-CN&conBW=1&conOP=1&cpa=1&dai=3&dis=0&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DGwyMa3rNDW5ZzeoO1fOeBxTBGL0lHzGevZEspmulpZfetnKRvhocuLqlDNxPyeN0UNRcd_cDvQgtUT4PQlTuoK%26wd%3D%26eqid%3Ddfdcafe100011a2600000005563bac6a&ltu=http%3A%2F%2Fwww.bubuko.com%2Finfodetail-1007293.html&lu_161=0&lunum=6&n=65035100_cpr&pat=6&pcs=1263x675&pih=0&pis=10000x10000&piw=0&ps=8330x131&psr=1280x800&pss=1263x8351&ptbg=90&ptp=0&ptt=0&qn=8764113a7b5840fc&rad=&rsi0=680&rsi1=250&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%23000000&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=tabcloud_skin_3&stid=5&td_id=1738566&titFF=%25E5%25BE%25AE%25E8%25BD%25AF%25E9%259B%2585%25E9%25BB%2591&titFS=14&titSU=0&titTA=left&tn=baiduCustNativeAD&tpr=1446750794660&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1738566&ti=Webdriver%E9%85%8D%E5%90%88Tesseract-OCR%20%E8%87%AA%E5%8A%A8%E8%AF%86%E5%88%AB%E7%AE%80%E5%8D%95%E7%9A%84%E9%AA%8C%E8%AF%81%E7%A0%81&tt=1446750794620.1978.2076.2076" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="padding: 0px;">
width="300" height="250" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_4" name="aswift_4" style="padding: 0px; left: 0px; position: absolute; top: 0px;">
友情链接
国之画  cnbeta   CSDN  博客园   百度统计   站长统计   

阳和移动开发   汇智网   易捷博客网   天码营   HarriesBlog   程序员客栈   情怀   
关于我们 - 联系我们 - 留言反馈
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
        

关闭
id="cproIframe2002" src="http://pos.baidu.com/acom?adn=1&at=6&aurl=&cad=1&ccd=24&ccw=1000&cec=UTF-8&cfv=0&ch=0&col=zh-CN&conOP=0&cpa=1&ct=1&dai=4&dis=0&fa=1&flw=1&ls=3&ltr=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DGwyMa3rNDW5ZzeoO1fOeBxTBGL0lHzGevZEspmulpZfetnKRvhocuLqlDNxPyeN0UNRcd_cDvQgtUT4PQlTuoK%26wd%3D%26eqid%3Ddfdcafe100011a2600000005563bac6a&ltu=http%3A%2F%2Fwww.bubuko.com%2Finfodetail-1007293.html&lunum=6&n=65035100_cpr&pcs=1263x675&pis=10000x10000&ps=8929x0&psr=1280x800&pss=1263x8930&pt=1&qn=082bed2e8a8d049f&rad=&rsi0=120&rsi1=270&rsi5=4&rsi6=-1&rsi7=0&rss0=&rss1=&rss2=&rss3=&rss4=&rss5=&rss6=&rss7=&scale=&skin=&td_id=1757709&tn=template_float_all_normal&tpr=1446750794660&ts=1&ww=0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1757709&distp=2002&ti=Webdriver%E9%85%8D%E5%90%88Tesseract-OCR%20%E8%87%AA%E5%8A%A8%E8%AF%86%E5%88%AB%E7%AE%80%E5%8D%95%E7%9A%84%E9%AA%8C%E8%AF%81%E7%A0%81&tt=1446750794620.3894.4019.4023&adclass=1&conW=120&conH=270" width="120" height="270" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="padding: 0px;">

验证码: 如下,在进行自动化测试,遇到验证码的问题,一般有两种方式相关推荐

  1. java actionchains,UI自动化测试:模拟组合按键操作的2种方式

    前言 在UI自动化测试的时候,可能会遇到键盘按键的组合操作,比如ctrl+c,ctrl+v等,类似于我们手动操作的快捷键.可以有两种方式实现:一种是selenium中自带的ActionChains结合 ...

  2. python自动输入验证码_python下的自动化测试--selenium 验证码输入问题

    之前一直在研究scrapy下数据抓取,在研究ajax数据抓取时碰巧研究了一下selenium,确实很实用,不过只做scrapy下的数据抓取,不怎么合适,一是性能的损耗,一直需要开一个浏览器,二是对于爬 ...

  3. Android获取短信验证码并自动填充的两种方式

    有些项目为了方便客户操作,减去客户输入短信验证码的时间,会要求安卓app能够获取收到的短信验证码并自动填充到输入框.所以,我整理了安卓获取短信验证码并自动填充输入框的两种方法,而且正式在项目中使用并无 ...

  4. 两种方式识别“传统”图片验证码

    目前,很多网站为了反爬都会采取各种各样的策略,比较简单粗暴的一种做法就是图片验证码,随着爬虫技术与反爬技术的演变,目前验证码也越来越复杂,比较高端的如Google的I'm not a robot,极验 ...

  5. android 短信验证码自动填写的两种方式

    https://blog.csdn.net/u010399316/article/details/48781319 https://www.01hai.com/note/av123438 https: ...

  6. java绘制图片验证码两种方式实现,点击【图片】刷新和点击【看不清换一张】刷新

    转载地址:https://blog.csdn.net/Jiang_Rong_Tao/article/details/78063295 点击打开链接 1.(看不清,换一张),点击刷新验证码 <im ...

  7. Appium 自动化测试配置wda的两种方式。

    tips:WebDriverAgent是Appium1.6.3以后版本新添加的模块,为了让appium与iPhone(基于xcuitest)设备进行通信而添加的.但是,这个模块在是一个独立的项目,在使 ...

  8. 自动化测试解决验证码问题

    现在的很多网站在登陆时都需要验证码,倘若遇到自动化测试时,怎么解决呢? 验证码大概有以下几种: 有的是图片验证码:图片上显示数字,汉字,英文数字以及算术题等: 有短信/邮箱发送验证码:一般为四位/六位 ...

  9. php点击图片更新验证码,thinkphp点击图片刷新验证码

    本类验证码功能刷新功能,涉及到两个刷新,一个是点击验证码图片刷新,另一个是输错验证码刷新,当然自己刷新那就不要说了,那是肯定会刷新的.thinkPHP框架里面内置了verify.class.php验证 ...

最新文章

  1. 国内少儿眼中的编程:“Coding即是代码”?
  2. DL之NN:利用(本地数据集50000张数据集)调用自定义神经网络network.py实现手写数字图片识别94%准确率
  3. 字符输出流写文本文件【Writer、FileWriter 、BufferedReader 】
  4. W3C标准的理解(2015.3.7)(陆续更新中)
  5. Orchard: module开发基础技术知识
  6. 人脸检测(四)--CART原理及实现
  7. QTP的那些事--打开脚本文件弹出“unexpected file format”错误
  8. Problem 1036 四塔问题
  9. HLI测试 涉及书籍
  10. PID实现水平姿态角控制
  11. 小鹿线前端课程怎么样
  12. 计算机云什么不同步,微云同步盘和腾讯微云的区别!微云只上传不同步-太平洋电脑网...
  13. 程序员真的干到35就干不动了吗?
  14. 重磅长文!先进院李骁健等人:在体神经界面技术的发展-从小到大规模记录
  15. 令人心酸至极的100个微瞬间
  16. 积目服务器维护,搭建经济高效的制作网络服务器群试验平台
  17. 互联网日报 | 贾跃亭乐视网股票流拍;东航组建“三亚国际航空”;苹果线上WWDC大会22日举办...
  18. 利用Windows 计划任务定时将本地文件复制到共享文件夹
  19. 网易云音乐前端模块动态下发系统
  20. 疑难杂症:同网段ping不通,跨网段建不了链,怎么破?

热门文章

  1. 【自动驾驶行业观察】Mobileye RSS
  2. python keyboard 键盘自动控制库
  3. 小白自学python的编程之路——实现进制数转换(十进制和二进制和八进制的转换)
  4. mysql 5.7 exe_mysql 5.7 版本 windows 安装
  5. 云计算网络工程师路线图_职业路线图:云工程师
  6. java asq_Java并发:AQS原理
  7. Threejs_贴图
  8. c语言个人通讯录管理系统实验报告_C语言个人通讯录管理系统课程设计报告
  9. 2011级-csdn-java-张侃—Spring(2)
  10. 基于FPGA的CAN总线控制器的设计(上)