python selenium-webdriver 登陸驗證碼的處理(十二)

不少系統爲了防止壞人,會增長各樣形式的驗證碼,作測試最頭痛的莫過於驗證碼的處理,驗證碼的處理通常分爲三種方法html

1.開發給咱們設置一個萬能的驗證碼;web

2.開發將驗證碼給屏蔽掉;json

3.本身識別圖片的上的千奇百怪的圖片,可是這樣的方法識別成功率不是特別的高,並且也不是對全部的均可以識別,只是識別一些簡單的驗證碼;api

4.能夠調用其餘的服務商的驗證碼識別的接口,我從阿里雲的雲市場找到一家驗證碼識別的接口,0.9元能夠調用大概是60次(0.01元20次,我不知道本身爲什麼傻乎乎的花了0.9元買了60次的規格);app

 

這裏主要使用到了pytesseract和PIL兩個模塊,首先咱們搭建一下環境post

pip install Pillow
pip install pytesseract 
因爲Python-tesseract是一個基於google's Tesseract-OCR的獨立封裝包,那麼咱們須要下載
Tesseract-OCR進行安裝,window下安裝記住須要配置環境變量

下面咱們直接看一下具體的實例測試

#-*- coding:utf-8 -*-
import time
from selenium import webdriver
from PIL import Image,ImageEnhance
import pytesseract

def get_auth_code(driver,codeEelement):
    '''獲取驗證碼'''
    driver.save_screenshot('login/login.png')  #截取登陸頁面
    imgSize = codeEelement.size   #獲取驗證碼圖片的大小
    imgLocation = imgElement.location #獲取驗證碼元素座標
    rangle = (int(imgLocation['x']),int(imgLocation['y']),int(imgLocation['x'] + imgSize['width']),int(imgLocation['y']+imgSize['height']))  #計算驗證碼總體座標
    login = Image.open("login/login.png")  
    frame4=login.crop(rangle)   #截取驗證碼圖片
    frame4.save('login/authcode.png')
    authcodeImg = Image.open('login/authcode.png')
    authCodeText = pytesseract.image_to_string(authcodeImg).strip()
    return authCodeText

def pandarola_login(driver,account,passwd,authCode):
    '''登陸pandarola系統'''
    driver.find_element_by_id('loginname').send_keys(account)
    driver.find_element_by_id('password').send_keys(passwd)
    driver.find_element_by_id('code').send_keys(authCode)
    driver.find_element_by_id('to-recover').click()
    time.sleep(2)
    title = driver.find_element_by_id('menuName-h').text  #獲取登陸的標題
    '''驗證是否登陸成功'''
    try:
        assert title == u'桌面'
        return '登陸成功'
    except AssertionError as e:
        return '登陸失敗'

if __name__ == '__main__':

    driver = webdriver.Chrome()
    driver.get('http://pandarola.pandadata.cn')
    driver.maximize_window()
    imgElement = driver.find_element_by_id('codeImg')
    authCodeText = get_auth_code(driver,imgElement)
    pandarola_login(driver,'admin','1',authCodeText)
    driver.quit()

因爲咱們系統屬於內部系統,驗證碼比較簡單,因此很輕鬆的識別了,可是有時候2和Z沒法識別,只要登陸失敗從新獲取再次登陸便可。畢竟本身寫的驗證碼識別,識別的成功的概率還時比較低,因此我這邊從阿里雲的雲市場找到了一家公司,用他們的接口來識別,最起碼公司的幾個系統的驗證碼問題所有解決了,不再用求開發了。ui

ps:google

  這裏包含了接口的說明文檔,大概能夠本身看一下,https://market.aliyun.com/products/57126001/cmapi014396.html#sku=yuncode839600006,我這邊演示下經過這個接口來識別驗證碼。阿里雲

#-*- coding:utf-8 -*-
import base64
import requests
import json
def read_picture_base64(fileNmae):
    '''驗證碼圖片 base64加密格式'''
    with open(fileNmae,'rb') as f:
        base64Picture = base64.b64encode(f.read())
    return base64Picture.decode()


def authcode_picture_convert_string(appCode,querys,base64Picture):  #appCode 接口的認證key,querys 驗證碼類型
    '''經過第三方結果獲取驗證碼'''
    header = {
        'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',   #根據API的要求,定義相對應的Content-Type
        "Authorization":"APPCODE "+ appCode
    }
    url = 'http://jisuyzmsb.market.alicloudapi.com/captcha/recognize'  #調用地址
    bodys = {'type':querys,'pic':base64Picture}   #請求參數
    res = requests.post(url,headers=header,data=bodys)
    return res.text

if __name__ == '__main__':
    appCode = '377e5f0fe10146ef9aa88bae756a3904'
    querys = 'e4'
    base64Picture = read_picture_base64('login/20170629232535.png')
    text = authcode_picture_convert_string(appCode,querys,base64Picture)
    authCode = json.loads(text)['result']['code']  #解析返回的結果
    print(authCode)
相關文章
相關標籤/搜索