Python selenium-webdriver Login Verification Code processing (12)

Source: Internet
Author: User

Many systems in order to prevent bad people, will add various forms of verification code, testing the most headache is the processing of verification Code, verification code processing generally divided into three ways

1. Developed to set up a universal verification code;

2. The development of the verification code to shield off;

3. Identify the pictures on the strange pictures, but such a method to identify the success rate is not particularly high, but also not all can be recognized, just identify some simple verification code;

The main use of the Pytesseract and PIL two modules, first we build the environment


Since Python-tesseract is a standalone package based on Google's TESSERACT-OCR, we need to download the TESSERACT-OCR to install it, install it under window and remember to configure environment variables

Let's take a look at specific examples

#-*-coding:utf-8-*-Import Time fromSeleniumImportWebdriver fromPILImportimage,imageenhanceImportpytesseractdefGet_auth_code (driver,codeeelement):" "Get Verification Code" "Driver.save_screenshot ('Login/login.png')#Intercept Login PageImgsize = Codeeelement.size#get the size of the CAPTCHA pictureImglocation = Imgelement.location#get the captcha element coordinatesRangle = (int (imglocation['x']), int (imglocation['y']), int (imglocation['x'] + imgsize['width']), int (imglocation['y']+imgsize['Height']))#calculate verification code overall coordinatesLogin = Image.open ("Login/login.png") Frame4=login.crop (Rangle)#Intercept CAPTCHA ImageFrame4.save ('Login/authcode.png') authcodeimg= Image.open ('Login/authcode.png') Authcodetext=pytesseract.image_to_string (authcodeimg). Strip ()returnAuthcodetextdefPandarola_login (driver,account,passwd,authcode):" "Login Pandarola System" "driver.find_element_by_id ('LoginName'). Send_keys (account) driver.find_element_by_id ('Password'). Send_keys (passwd) driver.find_element_by_id ('Code'). Send_keys (Authcode) driver.find_element_by_id ('To-recover'). Click () time.sleep (2) Title= driver.find_element_by_id ('menuname-h'). Text#get the title of the login    " "Verify that login is successful" "    Try:        asserttitle = = U'Desktop'        return 'Login Successful'    exceptAssertionerror as E:return 'Logon Failure'if __name__=='__main__': Driver=Webdriver. Chrome () Driver.get ('http://pandarola.pandadata.cn') Driver.maximize_window () ImgElement= driver.find_element_by_id ('codeimg') Authcodetext=Get_auth_code (driver,imgelement) pandarola_login (Driver,'Admin','1', Authcodetext) driver.quit ()

Because our system belongs to the internal system, the verification code is relatively simple, so it is easy to identify, but sometimes 2 and z is not recognized, as long as the login failed to regain access to login again.

Python selenium-webdriver Login Verification Code processing (12)

Related Article

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.