zoukankan      html  css  js  c++  java
  • 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_studychromedriverchromedriver')
        wb.get('http://192.168.10.233:8080/marsCloud/loginController.do?login')
        wb.maximize_window()
        wb.implicitly_wait(5)
    
        login(wb)
    
    main()
    
    
    
  • 相关阅读:
    SD_WebImage-03-多线程+下载任务放入非主线程执行
    NSOperationQueue_管理NSOperation-02-多线程
    CALayer小结-基本使用00-UI进阶
    XMPP-UI进阶-01
    XMPP总结-UI进阶-00
    UI控件总结-UI初级
    转场动画-01-day4
    暂停-开始动画-核心动画-08-day4
    核心动画-04-CALayer隐式动画
    Android开发技术周报 Issue#71
  • 原文地址:https://www.cnblogs.com/chenfei2928/p/12843643.html
Copyright © 2011-2022 走看看