網(wǎng)站首頁 編程語言 正文
對于web應(yīng)用程序來講,處于安全性考慮,在登錄的時候,都會設(shè)置驗證碼,
驗證碼的類型種類繁多,有圖片中辨別數(shù)字字母的,有點擊圖片中指定的文字的,也有算術(shù)計算結(jié)果的,再復(fù)雜一點就是滑動驗證的。
諸如此類的驗證碼,對我們的系統(tǒng)增加了安全性的保障,但是對于我們測試人員來講,在自動化測試的過程中,無疑是一個棘手的問題。
1、Web 自動化驗證碼解決方案
一般在我們測試過程中,登錄遇到上述的驗證碼的時候,有以下種解決方案:
- 第一種、讓開發(fā)去掉驗證碼
- 第二種、設(shè)置一個萬能的驗證碼
- 第三種、通過 cookie 繞過登錄
- 第四種、自動識別技術(shù)識別驗證碼
2、驗證碼解決方案
# 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)
# 找到驗證碼圖片,得到它的坐標(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))
# 得到驗證碼圖片
img.save(captcha_img_path)
# 打開驗證碼圖片
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)像素點顏色
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工具解析驗證碼圖片,生成文本
os.system(ocr_path)
# 讀取txt文件里面的驗證碼
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'
# 接口 - 識別驗證碼
def get_captcha(captcha_img_path, captcha_final_path, txt_path, ocr_path):
# 打開驗證碼圖片
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)像素點顏色
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工具解析驗證碼圖片,生成文本,【Tesseract-OCR必須和jpg的根目錄必須相同,如C盤、D盤!!!】
os.system(ocr_path)
# 讀取txt文件里面的驗證碼
with open(txt_path, 'r') as f:
if r.read():
t = f.read().strip()
# 去掉中間空格
if ' ' in t:
t = t.replace(' ', '')
# 如果是數(shù)字且長度為4,就返回數(shù)字,如果不是就返回 fail
if t.isdigit() and len(t) == 4:
return t
else:
return fail
原文鏈接:https://www.cnblogs.com/chenyablog/p/15162797.html
相關(guān)推薦
- 2022-09-18 ASP.NET實現(xiàn)文件上傳功能_實用技巧
- 2023-06-20 k8s應(yīng)用監(jiān)控探針詳解_云其它
- 2022-03-15 spark-submit hive SQL standards based authorizati
- 2023-01-03 Kotlin文件讀寫與SharedPreferences存儲功能實現(xiàn)方法_Android
- 2022-11-02 Python運(yùn)維自動化之paramiko模塊應(yīng)用實例_python
- 2022-06-17 C語言?詳細(xì)講解數(shù)組參數(shù)與指針參數(shù)_C 語言
- 2022-09-14 Python詳細(xì)講解淺拷貝與深拷貝的使用_python
- 2022-06-13 docker從安裝入門到應(yīng)用部署及私有倉庫搭建基礎(chǔ)命令_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支