当前位置:网站首页>在进行自动化测试,遇到验证码的问题,怎么办?
在进行自动化测试,遇到验证码的问题,怎么办?
2022-07-25 21:54:00 【小梧敲代码】
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>
<img src="http://csujwc.its.csu.edu.cn/sys/ValidateCode.aspx?t=1">
</br>
</body>
</html>
要识别验证码,首先得取得验证码,这两款采取对 页面元素部分截图的方式,首先获取整个页面的截图
然后找到页面元素坐标进行截取
/**
* This method for screen shot element
*
* @param driver
* @param element
* @param path
* @throws InterruptedException
*/
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();
}
}
截取完元素,就可以调用Tesseract-OCR生成text
// 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 ");
接下来通过java读取txt
/**
* This method for read TXT file
*
* @param filePath
*/
public static void 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);
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
整体代码如下:
1 package com.dbyl.tests;
2
3 import java.awt.Rectangle;
4 import java.awt.image.BufferedImage;
5 import java.io.BufferedReader;
6 import java.io.File;
7 import java.io.FileInputStream;
8 import java.io.IOException;
9 import java.io.InputStreamReader;
10 import java.io.Reader;
11 import java.util.concurrent.TimeUnit;
12
13 import javax.imageio.ImageIO;
14
15 import org.apache.commons.io.FileUtils;
16 import org.openqa.selenium.By;
17 import org.openqa.selenium.OutputType;
18 import org.openqa.selenium.Point;
19 import org.openqa.selenium.TakesScreenshot;
20 import org.openqa.selenium.WebDriver;
21 import org.openqa.selenium.WebElement;
22
23 import com.dbyl.libarary.utils.DriverFactory;
24
25 public class TesseractTest {
26
27 public static void main(String[] args) throws IOException,
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 file
51 *
52 * @param filePath
53 */
54 public static void readTextFile(String filePath) {
55 try {
56 String encoding = "GBK";
57 File file = new File(filePath);
58 if (file.isFile() && file.exists()) {
// 判断文件是否存在
59 InputStreamReader read = new InputStreamReader(
60 new FileInputStream(file), encoding);// 考虑到编码格式
61 BufferedReader bufferedReader = new BufferedReader(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 element
78 *
79 * @param driver
80 * @param element
81 * @param path
82 * @throws InterruptedException
83 */
84 public static void screenShotForElement(WebDriver driver,
85 WebElement element, String path) throws InterruptedException {
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 = new Rectangle(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, new File(path));
99 } catch (IOException e) {
100 e.printStackTrace();
101 }
102 }
103
104 }
最后感谢每一个认真阅读我文章的人,下面这个网盘链接也是我费了几天时间整理的非常全面的,希望也能帮助到有需要的你!

这些资料,对于想转行做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助……
如果你不想一个人野蛮生长,找不到系统的资料,问题得不到帮助,坚持几天便放弃的感受的话,可以点击下方小卡片加入我们群,大家可以一起讨论交流,里面会有各种软件测试资料和技术交流。
敲字不易,如果此文章对你有帮助的话,点个赞收个藏来个关注,给作者一个鼓励。也方便你下次能够快速查找。
自学推荐B站视频:
零基础转行软件测试:自学完软件测试,拿到了字节的测试岗offer,堪称B站最好的视频!
自动化测试进阶:已上岸华为,涨薪20K,2022最适合自学的python自动化测试教程,自己花16800买的,无偿分享

边栏推荐
- 立创EDA——我为什么要学EDA
- 立创EDA——器件的创建01-电阻(二)
- Preliminary study on Tesseract OCR
- 狗粮的成分
- 【饭谈】那些看似为公司着想,实际却让人无法理解的事(二:面试时的软素质“眼缘”)
- 【面试:并发篇24:多线程:综合练习】顺序控制
- New maixhub deployment (v831 and k210)
- Excuse me, how to deal with repeated consumption of MySQL data
- Collation of SQL statement exercises
- ZigBee development board (nxpzigbee Development)
猜你喜欢

6-18漏洞利用-后门连接
![[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决](/img/0b/da67b5a361a2cdfaf81568d34cf5f7.png)
[MAIXPY]kpu: load error:2005, ERR_READ_FILE: read file failed问题解决

立创EDA——器件的创建01-电阻(二)

Why do independent sellers like to do e-mail marketing? The original conversion rate can be improved so much!

Ijcai2022 meeting! Microsoft and other tutorials on domain generalization

919. Complete binary tree inserter: simple BFS application problem

这次龙蜥展区玩的新花样,看看是谁的 DNA 动了?

动画曲线天天用,你能自己整一个吗?看完这篇你就会了!

【leetcode天梯】链表 · 021 合并两个有序链表

Jmeter---设置代理录制请求
随机推荐
[redis underlying parsing] linked list type
动画曲线天天用,你能自己整一个吗?看完这篇你就会了!
Redis是什么?简述它的优缺点
C common set
How to implement distributed locks with redis?
Does the open source agreement have legal effect?
Animation curves are used every day. Can you make one by yourself? After reading this article, you will!
Unity metaverse (II), mixamo & animator hybrid tree and animation fusion
C语言左值和右值说明[通俗易懂]
919. 完全二叉树插入器 : 简单 BFS 运用题
Lichuang EDA -- creation of devices 01 resistance (II)
【面试:并发篇25:多线程:volatile】可见性
再次来光顾
Ability to choose
Redis configuration
分享|智慧消防应急管理平台解决方案(附PDF)
2022-07-18: what is the output of the following go language code? A:Groutine; B:Main; C:Goroutine; D:GoroutineMain。 package m
ES6 -- Deconstruction assignment
选择的能力
Creation and destruction of function stack frames