自动化测试验证码代码常用的四种方式
針對驗證碼有如下方法:
一、在產品沒有上線前,需要找開發先給web驗證碼留后門,也就是將驗證碼驗證先注釋掉
二、讓開發給web驗證碼留一個萬用驗證碼,只要輸入給的自定驗證碼數字,字母,就可以強制登錄
三、當有的頁面可以勾選保存用戶名,密碼可以通過Cookie跳過登錄驗證碼,使用抓包工具就行了
四、使用驗證碼識別技術
1.tesseract庫,先下載pip --default-timeout=500 install -U Pillow 安裝Pillow也就是安裝PIL庫處理圖像用的庫
2.安裝 pytesseract庫 pip install pytesseract
最后在百度下安裝tesseract 注意打開C:\Program Files\Tesseract-OCR復制路徑而且是反斜杠,在打開C:\Python\Python36\Lib\site-packages\pytesseract\pytesseract.py修改里面路徑
3.在百度安裝tesseract-OCR文件
配置環境變量
Path:C:\Program Files (x86)\Tesseract-OCR;
TESSDATA_PREFIX:C:\ProgramFiles(x86)\Tesseract-OCR;C:\ProgramFiles(x86)\Tesseract-OCR\tessdata
‘’’
@project:Selenium_SToom
@Time:2019-10-24 12:02
@Author:Felton
‘’’
導入包
from selenium import webdriver
import time
import pytesseract
from PIL import Image
from PIL import Image,ImageEnhance
打開火狐
driver=webdriver.Firefox()
輸入網站
driver.get(‘網站’)
time.sleep(2)
#截圖或驗證碼圖片保存地址
screenImg = r"D:\tupian\screenImg.png"
#登陸
driver.find_element_by_id(‘userid’).send_keys(‘賬號’)
driver.find_element_by_id(‘password’).send_keys(‘密碼’)
driver.find_element_by_id(‘image’).click()
#定位驗證碼圖片
driver.find_element_by_id(‘image’).get_attribute(‘src’)
#對驗證碼進行截圖
driver.get_screenshot_as_file(screenImg)
#定位驗證碼位置及大小
location = driver.find_element_by_id(‘image’).location
size = driver.find_element_by_id(‘image’).size
left = location[‘x’]
top = location[‘y’]
right = location[‘x’] + size[‘width’]
bottom = location[‘y’] + size[‘height’]
#從文件讀取截圖,截取驗證碼位置再次保存
img = Image.open(screenImg).crop((left,top,right,bottom))
img = img.convert(‘L’) #轉換模式:L | RGB
img = ImageEnhance.Contrast(img)#增強對比度
img = img.enhance(2.0) #增加飽和度
#保存修改后圖片
img.save(screenImg)
#讀取驗證碼
img = Image.open(screenImg)
注意:對安裝完成后需要使用pycharm進行驗證識別,如果成功識別驗證碼就可以了
導入包
import pytesseract
from PIL import Image
圖片地址
image = Image.open(r’D:/tupian/screeImg.png’)
code = pytesseract.image_to_string(image)
print(code)
總結
以上是生活随笔為你收集整理的自动化测试验证码代码常用的四种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在实时操作系统里随便写代码都能硬实时吗?
- 下一篇: 解决luyten 启动报错:this a