With this Python library, you can realize verification code recognition for free

When doing UI automation, whether it is APP or Web, we often encounter the need to enter a verification code on the login page. There are many ways to help us on the Internet, such as through the Baidu OCR interface or the open source interface of other platforms, but most of them There is a fee, which is very unfriendly to our personal study.

The editor has shared it before, and today I will introduce another Python open source library - OCR (ddddocr).

ddddocr
Github:https://kgithub.com/sml2h3/ddddocr

Installation: pip install ddddocr

Python requirements: <=3.9

Instructions:

# coding:utf-8
import ddddocr
# 对ddddocr进行实例化
ocr = ddddocr.DdddOcr()
# 读取文件
with open('test.png', 'rb') as f:
    # 读取图片信息
    img_bytes = f.read()
    
# 识别验证码
res = ocr.classification(img_bytes)
print(res)

As can be seen from the above code, the usage method is very simple. We only need to import the library, read the image information, and then identify the verification code. It is very practical. Next, follow the editor to give an example.

Example operations

URL

https://v3pro.houjiemeishi.com/PC/pages/login/login.html

Steps

1. Visit the above URL and use F12 to obtain the address of the verification code, then request the download through the interface and save it locally.

Insert image description here

2. Call the ddddocr library to identify the images saved in the above steps.

3. Locate the verification code output box and enter the verification code identified in the above steps.

Code operations

# coding:utf-8
import ddddocr
import requests
from selenium import webdriver
ocr = ddddocr.DdddOcr()
driver = webdriver.Chrome()
# 打开网址页面
driver.get('http://v3pro.houjiemeishi.com/PC/pages/login/login.html')
# 获取验证码图片的url地址
img_url = driver.find_element_by_class_name('codeImg').get_attribute('src')
# 通过接口请求url地址,并保存在本地
r = requests.get(img_url)
with open('1111.jpg', 'wb+') as f:  
    f.write(r.content)
# 再次读取图片信息
with open('1111.jpg', 'rb')as f2:
    img_bytes = f2.read()
# 通过ddddocr进行识别验证码
res = ocr.classification(img_bytes)
print('识别的验证码是:'+res)
# 进行输入验证码内容
driver.find_element_by_class_name('ipt2').send_keys(res)

After the code is written according to the steps, run the program and find the verification code input box, and the correct verification code content has been entered.

picture

Summarize

The editor briefly introduced how to use ddddocr and introduced how to identify the verification code through examples. You can try it yourself according to the company's projects. Thank you for reading. I hope it will be helpful to you.

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!

Guess you like

Origin blog.csdn.net/NHB456789/article/details/132855801