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


備注:

  • 測試不一定成功(圖像識別技術...),多次嘗試
  • 驅動:Chrome驅動
  • 圖片識別:百度AI提供(APP_ID、API_KEY、SECRET_KEY均由百度AI產生)
  • 圖像識別過程會在本地保存驗證碼圖片
  • 測試網站:https://pythonav.com/login/
```python import time from selenium import webdriver from aip import AipOcr

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

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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM