selenium-python-驗證碼-動態驗證碼


from selenium import webdriver
from PIL import Image
import pytesseract
import re

def security_code(wb):
    wb.save_screenshot('D:\\security_code\\printscreen.png')  # 截屏
    imgelement = wb.find_element_by_id('randCodeImage')  # 定位驗證碼
    location = imgelement.location  # 獲取驗證碼x,y坐標
    # print(location)
    size = imgelement.size  # 驗證碼高度、寬度、
    # print(size)

    left = int(location['x'] + 246)
    top = int(location['y'] + 75)
    right = int(location['x'] + size['width'] + 263)
    bottom = int(location['y'] + size['height'] + 83)

    img = Image.open('D:\\security_code\\printscreen.png').crop((left, top, right, bottom))  # 打開截圖
    img2 = img.convert('RGB')
    img2.save('D:\\security_code\\save.png')
    code = pytesseract.image_to_string(Image.open('D:\\security_code\\save.png'))
    print(code.strip())

    # 正則表達式去除空格或其他特殊符號
    b = ''
    for i in code.strip():
        pattern = re.compile(r'[a-zA-Z0-9]')
        m = pattern.search(i)
        if m != None:
            b += i
    print(b)
    return b


def login(wb):

    wb.find_element_by_id('userName').send_keys('admin')
    wb.find_element_by_id('password').send_keys('123456')
    yzm = security_code(wb)
    wb.find_element_by_id('randCode').send_keys(yzm)
    wb.find_element_by_id('but_login').click()

def main():
    wb = webdriver.Chrome(r'D:\develop_study\chromedriver\chromedriver')
    wb.get('http://192.168.10.233:8080/marsCloud/loginController.do?login')
    wb.maximize_window()
    wb.implicitly_wait(5)

    login(wb)

main()



免責聲明!

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



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