First, in the automation test, encountered the verification code processing method has the following two kinds:
1, search for development to remove the verification code or the use of universal Verification code
2. Automatic recognition using OCR
Here, method one as long as and research and development communication is OK.
Using Pytesseract automation identification, the general recognition rate is not too high, processing the general simple verification code or no problem, such as the following code:
The use is very simple, just the following steps:
Import pytesseract from Import imageimage=image.open ('new.jpg') vcode=pytesseract.image_to_ String (image)print Vcode
Second, but in the use of Python automated testing will encounter a difficulty, how to obtain the verification code, Python Webdriver API does not have such an interface. Baidu Search, on-line only Java solution, Python seems to have no, in this will be Python solution written down, for the needs of people to reference:
Workaround:
Obtaining the coordinates of the verification code from the page is worthwhile, using the PIL image module to intercept specific areas, the code is as follows:
Idea: Save Web Programs--navigate to verification code coordinates--and then verify code location from
fromPILImportImageImportpytesseract fromSeleniumImportWebdriverurl='http://xxxxx.com'Driver=Webdriver. Chrome () Driver.maximize_window ()#Maximize Your browserdriver.get (URL) driver.save_screenshot ('F://aa.png')#intercept the current page, which has the verification code we needImgElement = Driver.find_element_by_xpath ('//img[@src = "Rand!loginrand.action"]')#Location Verification CodeLocation = Imgelement.location#Get verification code x, y axis coordinatesSize=imgelement.size#get the length and width of the verification coderangle= (int (location['x']), int (location['y']), int (location['x']+size['width']), int (location['y']+size['Height']))#as the position coordinates we need to intercept.I=image.open ("F://aa.png")#OpenFrame4=i.crop (Rangle)#use the crop function of image to intercept the function we need againFrame4.save ('f://frame4.jpg') QQ=image.open ('f://frame4.jpg') Text=pytesseract.image_to_string (QQ). Strip ()#using image_to_string to identify verification codesPrintText
Reference modules:
Image module: Http://effbot.org/imagingbook/image.htm#tag-Image.Image.crop
Pytesseract Identification Verification Code method: http://www.waitalone.cn/python-php-ocr.html
Implementation of--python for specific Web zone methods in Webdriver