WebDriver implements the screenshot Method for a specific Web region-Python implementation, webdriver-python

Source: Internet
Author: User

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

 

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.