Verification code used in Python and related problems

Time:2021-7-19

When using Python for automation, we encounter the problem that login needs to identify the verification code. At this time, we use the pytesseact module and encounter an exception:

pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more information.

Solution:

1. Download the corresponding version of “Tesseract OCR”, download address:https://github.com/tesseract-ocr/tesseract/wiki    perhaps     https://github.com/UB-Mannheim/tesseract/wiki

Download File Name: tesseract-ocr-w64-setup-v5.0.0-alpha.20201127.exe

2. To install Tesseract OCR, please remember the installation path.

3. Configure environment variables:

(1) To configure the Tesseract OCR environment variable:

Configure the installation path of Tesseract OCR in the environment variable path. D:\Program Files\python38\Lib\site-packages\Tesseract-OCR 。 As shown in the figure below:

    

 

 

(2) Configure the testdata environment variable:

Add a new variable name: tessdata_ PREFIX。 The variable value is the path of testsdata under the Tesseract OCR path of the installation. That is, add testdata after the installation path of Tesseract OCR. D:\Program Files\python38\Lib\site-packages\Tesseract-OCR\tessdata。 As shown in the figure below

    

 

4. Then add pytesseract. Py to the source code

tesseract_cmd = 'tesseract'

To be amended as follows:

tesseract_cmd = r'D:\Program Files\python38\Lib\site-packages\Tesseract-OCR\tesseract.exe'

Rerun the script, OK

 

 

The login page is shown in the figure below.

 

Identification verification code attached:

from selenium import webdriver
from PIL import Image
import pytesseract

def readvcode():
    dr = webdriver.Chrome("D:\softwarePro\BrowserDriver\chromedriver.exe")
    dr.maximize_window()
    dr.get(url)
    dr.save_ Screenshot ('All. PNG ') # intercepts the current web page, which has the verification code we need
    imgelement = dr.find_element_by_class_name('imgcode')
    Location = imgelement. Location # get the X, Y axis coordinates of the verification code
    Size = imgelement. Size # get the length and width of the verification code
    rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']),
                  Int (Location ['y '] + size ['height']) #
    I = image. Open ("all. PNG") #
    Result = i.crop (range) # use the image's crop function to capture the area we need from the screenshot again
    result.save('result.png')
    text = pytesseract.image_to_string('result.png', 'eng').strip()

    dr.find_element_by_name("username").send_keys("admin")
    dr.find_element_by_name("password").send_keys("admin123")
    dr.find_element_by_name("validateCode").send_keys(text)
    dr.find_element_by_id("btnSubmit").click()

if __name__ == '__main__':
    readvcode()

 

The captcha with noise or dash class will be updated later.

Recommended Today

MSSQL · query and sort the records of all tables in a database

Reading time |0.27 minutes word count |444 characters primary coverage |1. Introduction & background “MSSQL · query and sort the records of all tables in a database” Author | SCscHero Writing time | 2021/7/13 PM10:0 Article type |Series Degree of completion |Completed motto Every great cause has a trivial beginning. 1、 Introduction & background    Completion: […]