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()
    
    
    
  • 相关阅读:
    Tsinghua dsa mooc pa1
    sctf pwn400
    sctf pwn300
    Calendar的add()方法介绍
    SQL语句基础知识
    oracle数据库中的表设置主键自增
    SQL to_char,to_date日期字符串转换问题
    SQL where 1=1的作用
    SVN使用教程总结
    由多次使用Statement实例引起的Result set already closed异常的解决方案
  • 原文地址:https://www.cnblogs.com/chenfei2928/p/12843643.html
Copyright © 2011-2022 走看看