WebDriver implementation for specific Web region methods-Python implementation, webdriver-python
1. in automated testing, there are two methods to handle verification codes:
1. Find the developer to remove the verification code or use the universal verification code.
2. Automatic OCR
Here, method 1 only needs to communicate with R & D.
Use pytesseract for automatic identification. Generally, the recognition rate is not too high. It is okay to process simple verification codes, for example, the following verification code:
It is very easy to use and only takes the following steps:
import pytesseractfrom PIL import Imageimage=Image.open('new.jpg')vcode=pytesseract.image_to_string(image)print vcode
II. However, there will be a difficulty in using python automated testing. How can I obtain the verification code? The python webdriver API does not have such an interface. Baidu found that there is only a java solution on the Internet, and python does not seem to exist. Here we will write down the python solution for reference:
Solution:
Obtain the coordinates of the verification code from the page. Use the PIL Image module to capture a specific area. The Code is as follows:
Train of Thought: Save the web program --> locate the verification code coordinate --> locate the verification code from it
From PIL import Imageimport pytesseractfrom selenium import webdriverurl = 'HTTP: // xxxxx.com 'driver = webdriver. chrome () driver. maximize_window () # maximize the driver for the browser. get (url) driver. save_screenshot ('f: // aa.png ') # capture the current webpage. This webpage has the required verification code imgelement = driver. find_element_by_xpath ('// img [@ src = "rand! LoginRand. action "] ') # locate the verification code location = imgelement. location # obtain the verification code x, Y axis coordinate size = imgelement. size # rangle = (int (location ['X']), int (location ['y']), int (location ['X'] + size ['width']), int (location ['y'] + size ['height']) # Write the coordinate I = Image. open ("f: // aa.png") # open frame4 = I. crop (rangle) # Use the Image crop function to capture the required function frame4.save ('f: // frame4.jpg ') qq = Image. open ('f: // frame4.jpg ') text = pytesseract. image_to_string (qq ). strip () # Use image_to_string to identify the verification code print text
Reference module:
Image module: http://effbot.org/imagingbook/image.htm#tag-Image.Image.crop
Pytesseract Verification Code Recognition Method: http://www.waitalone.cn/python-php-ocr.html