Python based Selenium automation (3)-Enable verification code interception and recognition __python

Source: Internet
Author: User

These days are really busy smoking, a lot of projects piled together, night and day overtime. Plus some laziness, learning progress has not been much progress. This article mainly introduces the previous period of time to implement a function, I hope there is a need to get a little inspiration.

Based on the UI layer of automation, there are some pits inside that almost everyone will encounter, one of which is registering or signing in with a CAPTCHA. Generated dynamically because of the validation code. Each visit will be different, so it would be a hassle to use a script to log in.
Some of the solutions are:
1, for the company's internal projects have two methods,
(1) Set a universal verification code, as long as each fill out this verification code can be validated through
(2) The mobile phone number is set to White list, as long as the input of a specific mobile phone number, the verification code is not verified
2, for external projects, you can use the following methods
(3) Intercept the verification code part and use the image recognition technology to identify
This article mainly introduces (3) the realization process. For reference only
Steps can be divided into intercept verification Code + recognition Verification code
First, intercept the verification code
The Selenium Webdriver API directly provides a screenshot of the method. But it is full screen, to achieve the interception of elements, you need to go around a detours
1, get to the size of the element, the coordinates of the element
2. Intercepting whole screen
3, according to the elements of the coordinates and size, locate the area to be trimmed
4. Use image Library to trim the area of the element
Python Code implementation:

        Driver.get_screenshot_as_file (' a.jpg ')
        location = driver.find_element_by_id (' validate-img '). Location
        Size = driver.find_element_by_id (' validate-img '). Size left
        = location[' x '] top
        =  location[' y ']
        right = location[' x '] + size[' width ']
        bottom = location[' y '] + size[' height ']
        a = Image.open ("a.jpg")
        im = A.C ROP ((left,top,right,bottom))
        im.save (' a.jpg ')
        time.sleep (1)

The code doesn't have a comment section, which explains the four steps above

Second, identify the verification code
This article does not design how to implement the verification code, this part of the task I chose is a Third-party API to achieve. (if necessary, you can search for "Super Eagle Verification Code recognition") after all, the surgery industry has specialization, if you want to do OCR recognition of the classmate, this article does not have any reference value, this skip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.