程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Cracking graphic verification code of Python automatic test

編輯:Python

about web For applications , In terms of safety , At login time , Will set the verification code ,
There are many types of verification codes , There are pictures that distinguish numbers and letters , Click on the text specified in the picture , There are also arithmetic results , Another complication is the sliding verification .
Verification codes like this , It adds security guarantee to our system , But for our testers , In the process of automated testing , There is no doubt that it is a difficult problem .

1、Web Automated verification code solution

Generally, during our testing , When the login encounters the above verification code , There are the following solutions :

The first one is 、 Let developers remove the verification code

The second kind 、 Set a universal verification code

The third kind of 、 adopt cookie Bypass login

A fourth 、 Automatic identification technology identification verification code

2、 Captcha solutions # coding:utf-8import osimport subprocessfrom PIL import Imagedef get_captcha(driver, captcha_id, full_screen_img_path, captcha_img_path, captcha_final_path, txt_path, ocr_path): # Screenshot of browser interface driver.save_screenshot(full_screen_img_path) # Find the verification code image , Get its coordinates element = driver.find_element_by_id(captcha_id) left = element.location['x'] top = element.location['y'] right = element.location['x'] + element.size['width'] bottom = element.location['y'] + element.size['height'] left, top, right, bottom = int(left), int(top), int(right), int(bottom) img = Image.open(full_screen_img_path) img = img.crop((left, top, right, bottom)) # Get the captcha picture img.save(captcha_img_path) # Open captcha image img = Image.open(captcha_img_path) # color histogram ,255 Color ,255 White # Create a new picture ( The size is the same as the original , The background color is 255 white ) img_new = Image.new('P', img.size, 255) for x in range(img.size[1]): for y in range(img.size[0]): # Traversing the image xy Coordinate pixel color pix = img.getpixel((y, x)) # print(pix) # Color yourself ,r=0,g=0,b>0 For blue if pix[0] < 20 and pix[1] < 20 and pix[2] > 50: # Put the traversal result on the new picture ,0 For transparency , Opaque img_new.putpixel((y, x), 0) img_new.save(captcha_final_path, format='png') # adopt tesseract The tool parses the captcha image , The generation of textual os.system(ocr_path) # Read txt The verification code in the file with open(txt_path, 'r') as f: if f.read(): t = f.read().strip() # Remove the middle space if ' ' in t: t = t.replace(' ', '') if t.isdigit() and len(t) == 4: return t else: return 'fail'def check_resp(result, msg): if msg in result: return 'pass' else: return 'failed'# Interface - Identification verification code def get_captcha(captcha_img_path, captcha_final_path, txt_path, ocr_path): # Open captcha image img = Image.open(captcha_img_path) # Create a new picture ( The size is the same as the original , The background color is 255 white ) img_new = Image.new('P', img.size, 55) for x in range(img.size[1]): for y in range(img.size[0]): # Traversing the image xy Coordinate pixel color pix = img.getpixel((y, x)) # print(pix) # Color yourself ,r=0,g=0,b>0 For blue if pix[0] < 20 and pix[1] < 20 and pix[2] > 50: # Put the traversal result on the new picture ,0 For transparency , Opaque img_new.putpixel((y, x), 0) img_new.save(captcha_final_path, format='png') # adopt tesseract The tool parses the captcha image , The generation of textual ,【Tesseract-OCR It has to be with jpg The root directory of must be the same , Such as C disc 、D disc !!!】 os.system(ocr_path) # Read txt The verification code in the file with open(txt_path, 'r') as f: if r.read(): t = f.read().strip() # Remove the middle space if ' ' in t: t = t.replace(' ', '') # If it is a number and the length is 4, Just return the number , If not, go back fail if t.isdigit() and len(t) == 4: return t else: return fail

This is about python The article on cracking the graphic verification code is introduced here . I hope it will be helpful for your study , I also hope you can support the software development network .



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved