Selenium+dddocr easily solves automatic Web verification code recognition!

Hello everyone, I am Xiao Hao. Today I would like to recommend a verification code recognition artifact to you: dddocr.

1 Introduction

dddocr is an OCR (Optical Character Recognition, optical character recognition) library based on deep learning, used to recognize text in pictures. It can recognize various types of text, including print, handwriting, tables, barcodes, and more. The dddocr library uses advanced models such as deep convolutional neural networks (CNN) and recurrent neural networks (RNN), which have high accuracy and stability.

The development and application of text recognition can be easily carried out using the dddocr library. It provides a simple and easy-to-use API interface that can receive images as input and return recognition results. Users only need to pass the image to be recognized into the API interface of the dddocr library to obtain the recognized text information. At the same time, the dddocr library also supports batch processing of multiple images, providing multi-threading and distributed processing functions, which can improve recognition speed and efficiency.

The dddocr library can be widely used in various scenarios, such as document digitization, image retrieval, automated office, etc. It can help users quickly and accurately extract text information from pictures to facilitate subsequent processing and analysis. In practical applications, the dddocr library has been widely used in finance, medical, logistics and other fields, and has achieved good results.

2. Basic use

Install:pip install dddocr

The dddocr library can be used to support the identification of different types of verification codes.

Example 1: English letter verification code recognition

import dddocr

def recognize_letter_captcha(image_path):
    ocr = dddocr.DddOcr()
    result = ocr.classification(image_path, model_type='letter')
    return result

image_path = 'letter_captcha.png'
result = recognize_letter_captcha(image_path)
print(result)

Example 2: Digital verification code identification

import dddocr

def recognize_number_captcha(image_path):
    ocr = dddocr.DddOcr()
    result = ocr.classification(image_path, model_type='number')
    return result

image_path = 'number_captcha.png'
result = recognize_number_captcha(image_path)
print(result)

Example 3: Mixed verification code recognition

import dddocr

def recognize_mixed_captcha(image_path):
    ocr = dddocr.DddOcr()
    result = ocr.classification(image_path, model_type='mixed')
    return result

image_path = 'mixed_captcha.png'
result = recognize_mixed_captcha(image_path)
print(result)

Example 4: Sliding verification code recognition

import dddocr

def recognize_slide_captcha(image_path):
    ocr = dddocr.DddOcr()
    result = ocr.slide_captcha(image_path)
    return result

image_path = 'slide_captcha.png'
result = recognize_slide_captcha(image_path)
print(result)

Example 5: Chinese verification code recognition

import dddocr

def recognize_chinese_captcha(image_path):
    ocr = dddocr.DddOcr()
    result = ocr.classification(image_path, model_type='chinese')
    return result

image_path = 'chinese_captcha.png'
result = recognize_chinese_captcha(image_path)
print(result)

In the above example, image_path is the path of the verification code image to be recognized, and different types of verification codes are identified by calling different recognition functions. Each example creates an instance of dddocr and then calls the corresponding method for identification. The recognition result will be returned in the form of a string. In actual applications, parameter adjustment and model training may need to be performed according to specific circumstances to improve recognition accuracy.

现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036

3. selenium+dddorc automated login identification verification code

When using selenium and dddocr for automatic login, you can identify the verification code through the following steps:

  1. Install selenium and dddocr libraries:
  pip install selenium
  pip install dddocr
  1. Import necessary libraries and modules:
   from selenium import webdriver
   from selenium.webdriver.common.by import By
   from selenium.webdriver.support.ui import WebDriverWait
   from selenium.webdriver.support import expected_conditions as EC
   import dddocr
  1. Create an instance of dddocr:
  ocr = dddocr.DddOcr()
  1. Use selenium to open the login page and find the verification code image element:
   driver = webdriver.Chrome()
   driver.get('https://example.com/login')
   captcha_image = driver.find_element(By.ID, 'captcha-image')
  1. Get the src attribute of the verification code image, and download and save it locally:
  captcha_image_src = captcha_image.get_attribute('src')
  driver.get_screenshot_as_file('screenshot.png')
  1. Use dddocr to identify the verification code:
   result = ocr.classification('screenshot.png', model_type='mixed')
   captcha_code = result[0]['text']
  1. Find the verification code input box on the login page and enter the recognized verification code:
   captcha_input = driver.find_element(By.ID, 'captcha-input')
   captcha_input.send_keys(captcha_code)
  1. Enter additional login information and submit the form:
   username_input = driver.find_element(By.ID, 'username-input')
   password_input = driver.find_element(By.ID, 'password-input')
   username_input.send_keys('your_username')
   password_input.send_keys('your_password')
   submit_button = driver.find_element(By.ID, 'submit-button')
   submit_button.click()

Complete code example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import dddocr

ocr = dddocr.DddOcr()

driver = webdriver.Chrome()
driver.get('https://example.com/login')

captcha_image = driver.find_element(By.ID, 'captcha-image')
captcha_image_src = captcha_image.get_attribute('src')
driver.get_screenshot_as_file('screenshot.png')

result = ocr.classification('screenshot.png', model_type='mixed')
captcha_code = result[0]['text']

captcha_input = driver.find_element(By.ID, 'captcha-input')
captcha_input.send_keys(captcha_code)

username_input = driver.find_element(By.ID, 'username-input')
password_input = driver.find_element(By.ID, 'password-input')
username_input.send_keys('your_username')
password_input.send_keys('your_password')

submit_button = driver.find_element(By.ID, 'submit-button')
submit_button.click()

In the above code example, it is assumed that the ID of the verification code image element on the login page is 'captcha-image', the ID of the verification code input box is 'captcha-input', the ID of the username input box is 'username-input', and the password input The box's id is 'password-input' and the login button's id is 'submit-button'. Depending on the actual situation, these id values ​​need to be replaced with actual page element ids.

Note: The above example only applies when the verification code image is directly embedded in the page in the form of an img tag.

4. How to identify the verification code loaded through Ajax request

If the verification code is loaded via an Ajax request, you can identify the verification code by following these steps:

  1. Use selenium to open the login page and wait for the verification code image to load:
   driver = webdriver.Chrome()
   driver.get('https://example.com/login')

   wait = WebDriverWait(driver, 10)
   captcha_image = wait.until(EC.presence_of_element_located((By.ID, 'captcha-image')))
  1. Execute the JavaScript code to obtain the base64 encoding of the verification code image:
   captcha_image_base64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(21);", captcha_image)
  1. Decode base64 encoding into images and save them locally:
   with open('captcha.png', 'wb') as f:
       f.write(base64.b64decode(captcha_image_base64))
  1. Use dddocr to identify the verification code:
  result = ocr.classification('captcha.png', model_type='mixed')
  captcha_code = result[0]['text']
  1. Find the verification code input box on the login page and enter the recognized verification code:
   captcha_input = driver.find_element(By.ID, 'captcha-input')
   captcha_input.send_keys(captcha_code)
  1. Enter additional login information and submit the form:
   username_input = driver.find_element(By.ID, 'username-input')
   password_input = driver.find_element(By.ID, 'password-input')
   username_input.send_keys('your_username')
   password_input.send_keys('your_password')

   submit_button = driver.find_element(By.ID, 'submit-button')
   submit_button.click()

Complete code example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import dddocr
import base64

ocr = dddocr.DddOcr()

driver = webdriver.Chrome()
driver.get('https://example.com/login')

wait = WebDriverWait(driver, 10)
captcha_image = wait.until(EC.presence_of_element_located((By.ID, 'captcha-image')))

captcha_image_base64 = driver.execute_script("return arguments[0].toDataURL('image/png').substring(21);", captcha_image)

with open('captcha.png', 'wb') as f:
    f.write(base64.b64decode(captcha_image_base64))

result = ocr.classification('captcha.png', model_type='mixed')
captcha_code = result[0]['text']

captcha_input = driver.find_element(By.ID, 'captcha-input')
captcha_input.send_keys(captcha_code)

username_input = driver.find_element(By.ID, 'username-input')
password_input = driver.find_element(By.ID, 'password-input')
username_input.send_keys('your_username')
password_input.send_keys('your_password')

submit_button = driver.find_element(By.ID, 'submit-button')
submit_button.click()

In the above code example, it is assumed that the ID of the verification code image element on the login page is 'captcha-image', the ID of the verification code input box is 'captcha-input', the ID of the username input box is 'username-input', and the password input The box's id is 'password-input' and the login button's id is 'submit-button'. Depending on the actual situation, these id values ​​need to be replaced with actual page element ids.

Note: The above example only applies to the case where the verification code image is loaded through an Ajax request and the base64 encoding is returned. If the verification code image is loaded through other methods, or data in other formats (such as the URL of the image) is returned, you need to handle it accordingly according to the specific situation.

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/jiangjunsss/article/details/132833989