--python implementation of the screenshot method in Webdriver for specific web areas

Source: Internet
Author: User

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

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.