Selenium+Tesseract cracking login verification code


The type of login verification code I cracked is the type of mathematical formula, i.e... does it look simple, but Tesseract's recognition rate of this kind of picture is also very low. If I want to improve the recognition rate in the later stage, I think one is to reduce the noise and sharpen the picture, and the other is to train Tesseract in advance. I recommend jtesboxeditor as the training tool. You can learn about it if you are interested.


1. Install Tesseract

1.1 download Tesseract. Address: Windows Installer made with MinGW-w64 , download and install directly. After the installation, configure the environment variables. Edit the path in the system variable and add the following environment variables.

C:\Program Files(x86)\Tesseract-OCR

After the installation is complete, enter in the cmd command line window:

tesseract -v

The following appears to prove that the installation was successful.

1.2 install pyteseract

pip install pytesseract


2 identification code

2.1 identification logic. First, use selenium to find the verification code label in the browser, then save the screenshot, and then use tesseract to identify the image. If the recognition fails, use selenium to click on the verification code picture, regenerate a verification code, and repeat the previous recognition logic. If the recognition is successful, use eval to calculate the value of the formula, fill in and click the login button.

2.2 code

import time
import pytesseract
from selenium.webdriver import Chrome
from PIL import Image

driver = Chrome(executable_path=r'D:\Chrome Downloads\chromedriver.exe')  # Storage path of chrome driver
url = ""
print("Start crawling, url by{}".format(url))

# Find the input tag of username and fill in the username
username = driver.find_element_by_id('txtUserName')
username.send_keys('your username')

# Find the input tag of password and fill in the password. If the password is cleared when you click the verification code again, the code should be placed in the while loop
password = driver.find_element_by_id('pwd')
password.send_keys('your password')

# 20 attempts
times = 0
while True:
        element = driver.find_element_by_id('validimg')
        # The picture of the verification code requested each time is different, so you can only take a screenshot and save it locally.
        # In windows system, pay attention to page scaling. If it is 150%, then each item is * 1.5. linux system does not need
        left = element.location['x']*1.5
        top = element.location['y']*1.5
        right = element.location['x']*1.5 + element.size['width']*1.5
        bottom = element.location['y']*1.5 + element.size['height']*1.5

        im ='validImage.png')
        im = im.crop((left, top, right, bottom))  # Matting'validImage.png')

        data = pytesseract.image_to_string('validImage.png'))
        if len(data) < 3:  # Unrecognized

        if len(data) == 4:  # Identification band = number, remove = number
            data = data[:3]

        if data[1] == ".":  # -No. is often identified as. And replaced with - No
            data[1] = "-"

        print("ocr Identification results", data)
        valid_code = driver.find_element_by_id('validcode')

        btnLogin = driver.find_element_by_id('btnLogin')

        # Get cookie s
        login_cookie = driver.get_cookie('ASP.NET_SessionId')
        total_cookie = 'ASP.NET_SessionId={}; autoLogin=null; user=null; pwd=null'.format(login_cookie['value'])

    except Exception as e:
        times += 1
        print("Failed to crack the verification code. Try again{}second".format(times))
        if times == 20:
            raise RuntimeError
# linux system should pay attention to exit from chrome driver. Otherwise, if executed multiple times, a large number of chrome driver processes will be generated.

The results are as follows:

One of the comparison pits is that the windows system displays scaling. You can see it in the desktop right-click display settings zoom and layout. If the scale is 150%, then in the screenshot, each parameter must be * 1.5, otherwise the desired verification code cannot be cut.

Tags: Python Windows Selenium Linux pip

Posted on Thu, 25 Jun 2020 00:25:22 -0400 by tjhilder