Selenium之自動登陸(識別驗證碼版)

<p>備註:</p> <ul> <li>測試不必定成功(圖像識別技術...),屢次嘗試</li> <li>驅動:Chrome驅動</li> <li>圖片識別:百度AI提供(APP_ID、API_KEY、SECRET_KEY均由百度AI產生)</li> <li>圖像識別過程會在本地保存驗證碼圖片</li> <li>測試網站:<a href="https://pythonav.com/login/">https://pythonav.com/login/</a></li> </ul> ```python import time from selenium import webdriver from aip import AipOcrpython

def initial(): """ 初始化鏈接 """ APP_ID = '16611607' API_KEY = 'wAIXfXOUS8ztLa4FrK3rZex1' SECRET_KEY = '3b8nvjSGUZq0LPC18VVAizKYRBbny6Mq' return AipOcr(APP_ID, API_KEY, SECRET_KEY)web

def get_file_content(filePath): """ 讀取圖片 """ with open(filePath, 'rb') as f: return f.read()測試

def selnium_msg(): driver = webdriver.Chrome() try: driver.get('https://pythonav.com/login/') time.sleep(1)網站

# 輸入用戶名/密碼
    driver.find_element_by_id('id_username').send_keys('用戶名')
    time.sleep(2)
    driver.find_element_by_id('id_password').send_keys('密碼')
    time.sleep(2)

    # 驗證碼識別
    image = driver.find_element_by_id('image_code')
    file_path = 'a.png'
    image.screenshot(file_path)

    # 百度ai相關
    client = initial()
    image = get_file_content(file_path)
    res = client.basicGeneral(image)  # 調用通用文字識別, 圖片參數爲本地圖片
    # res2 = client.basicAccurate(image)  # 調用通用文字識別(高精度版)

    driver.find_element_by_id('id_code').send_keys(res['words_result'][0]['words'])
    driver.find_element_by_class_name('btn-primary').click()
    time.sleep(3)
except Exception as e:
    print(e)
finally:
    driver.quit()

if name == 'main': selnium_msg()ui

相關文章
相關標籤/搜索