欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python自動(dòng)化測(cè)試之破解圖文驗(yàn)證碼

 更新時(shí)間:2022年07月01日 15:03:59   作者:小旭2021  
這篇文章介紹了python自動(dòng)化測(cè)試之破解圖文驗(yàn)證碼的解決方案,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

對(duì)于web應(yīng)用程序來講,處于安全性考慮,在登錄的時(shí)候,都會(huì)設(shè)置驗(yàn)證碼,
驗(yàn)證碼的類型種類繁多,有圖片中辨別數(shù)字字母的,有點(diǎn)擊圖片中指定的文字的,也有算術(shù)計(jì)算結(jié)果的,再?gòu)?fù)雜一點(diǎn)就是滑動(dòng)驗(yàn)證的。
諸如此類的驗(yàn)證碼,對(duì)我們的系統(tǒng)增加了安全性的保障,但是對(duì)于我們測(cè)試人員來講,在自動(dòng)化測(cè)試的過程中,無疑是一個(gè)棘手的問題。

1、Web 自動(dòng)化驗(yàn)證碼解決方案

一般在我們測(cè)試過程中,登錄遇到上述的驗(yàn)證碼的時(shí)候,有以下種解決方案:

  • 第一種、讓開發(fā)去掉驗(yàn)證碼
  • 第二種、設(shè)置一個(gè)萬能的驗(yàn)證碼
  • 第三種、通過 cookie 繞過登錄
  • 第四種、自動(dòng)識(shí)別技術(shù)識(shí)別驗(yàn)證碼

2、驗(yàn)證碼解決方案

# coding:utf-8
import os
import subprocess
from PIL import Image
 
 
def get_captcha(driver, captcha_id, full_screen_img_path, captcha_img_path, captcha_final_path, txt_path, ocr_path):
    # 瀏覽器界面截圖
    driver.save_screenshot(full_screen_img_path)
    # 找到驗(yàn)證碼圖片,得到它的坐標(biāo)
    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))
    # 得到驗(yàn)證碼圖片
    img.save(captcha_img_path)
    # 打開驗(yàn)證碼圖片
    img = Image.open(captcha_img_path)
    # 顏色直方圖,255種顏色,255為白色
    # 新建一張圖片(大小和原圖大小相同,背景顏色為255白色)
    img_new = Image.new('P', img.size, 255)
    for x in range(img.size[1]):
        for y in range(img.size[0]):
            # 遍歷圖片的xy坐標(biāo)像素點(diǎn)顏色
            pix = img.getpixel((y, x))
            # print(pix)
            # 自己調(diào)色,r=0,g=0,b>0為藍(lán)色
            if pix[0] < 20 and pix[1] < 20 and pix[2] > 50:
                # 把遍歷的結(jié)果放到新圖片上,0為透明度,不透明
                img_new.putpixel((y, x), 0)
    img_new.save(captcha_final_path, format='png')
 
    # 通過tesseract工具解析驗(yàn)證碼圖片,生成文本
    os.system(ocr_path)
 
    # 讀取txt文件里面的驗(yàn)證碼
    with open(txt_path, 'r') as f:
        if f.read():
            t = f.read().strip()
            # 去掉中間空格
            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'
 
 
# 接口 - 識(shí)別驗(yàn)證碼
def get_captcha(captcha_img_path, captcha_final_path, txt_path, ocr_path):
 
    # 打開驗(yàn)證碼圖片
    img = Image.open(captcha_img_path)
 
    # 新建一張圖片(大小和原圖大小相同,背景顏色為255白色)
    img_new = Image.new('P', img.size, 55)
    for x in range(img.size[1]):
        for y in range(img.size[0]):
            # 遍歷圖片的xy坐標(biāo)像素點(diǎn)顏色
            pix = img.getpixel((y, x))
            # print(pix)
            # 自己調(diào)色,r=0,g=0,b>0為藍(lán)色
            if pix[0] < 20 and pix[1] < 20 and pix[2] > 50:
                # 把遍歷的結(jié)果放到新圖片上,0為透明度,不透明
                img_new.putpixel((y, x), 0)
    img_new.save(captcha_final_path, format='png')
 
    # 通過tesseract工具解析驗(yàn)證碼圖片,生成文本,【Tesseract-OCR必須和jpg的根目錄必須相同,如C盤、D盤?。?!】
    os.system(ocr_path)
 
    # 讀取txt文件里面的驗(yàn)證碼
    with open(txt_path, 'r') as f:
        if r.read():
            t = f.read().strip()
            # 去掉中間空格
            if ' ' in t:
                t = t.replace(' ', '')
            # 如果是數(shù)字且長(zhǎng)度為4,就返回?cái)?shù)字,如果不是就返回 fail
            if t.isdigit() and len(t) == 4:
                return t
            else:
                return fail

到此這篇關(guān)于python破解圖文驗(yàn)證碼的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論