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)