gcgc

思路:

  1、根据xpath获取登录页面验证码元素区域的位置坐标

  2、根据位置坐标截取验证码图片

  3、识别验证码文本,再使用UI或者接口登录

 

步骤:

1、安装tesseract,及pytesseract

  参考:https://blog.csdn.net/qq_38900441/article/details/82823312

2、编写裁剪、识别验证码、登录脚本---station_web_login.py

# coding:utf-8

from PIL import Image
from selenium import webdriver
import pytesseract
import time

def cut_image():
    # 截图当前页面
    driver.save_screenshot(\'station_web_login.png\')

    # 定位到该元素,获取位置信息
    img = driver.find_element_by_xpath(\'//img[@id="vildateImg"]\')
    location = img.location
    size = img.size
    left = location[\'x\']
    top = location[\'y\']
    right = left + size[\'width\']
    bottom = top + size[\'height\']

    # 裁剪图片
    img = Image.open(\'station_web_login.png\')
    cropped = img.crop((left, top, right, bottom))
    cropped.save(\'station_web_login_checkCode.png\')

    # 识别图片
    text = pytesseract.image_to_string(Image.open(\'station_web_login_checkCode.png\').resize((300, 100)))

    # 模拟登录
    driver.find_element_by_xpath(\'//input[@id="username"]\').send_keys(\'****\')
    driver.find_element_by_xpath(\'//input[@id="password"]\').send_keys(\'*******\')
    driver.find_element_by_xpath(\'//input[@id="rand"]\').send_keys(text)
    driver.find_element_by_xpath(\'//button[@id="submitlogin"]\').click()
    # return text
    # 获取页面源码
    global source
    source = driver.page_source


def login():
    """
    登录并获取TOKEN和JSESSIONID
    :return:
    """
    global driver
    driver = webdriver.Chrome()
    url = \'http://*******/login\'
    driver.maximize_window()
    driver.get(url)
    # 等待两秒截图更清晰
    time.sleep(2)
    # 裁剪图片
    cut_image()

    if \'验证码不能为空或错误!\' in source:
        driver.refresh()
        cut_image()
    if \'欢迎使用\' in source:

        # 获取cookies,用于后续接口访问鉴权
        all_cookies = driver.get_cookies()
        TOKEN = all_cookies[0][\'value\']
        JSESSIONID = all_cookies[1][\'value\']

        driver.quit()
        return TOKEN, JSESSIONID

3、将station_web_login.py文件放置到框架中

然后在需要的地方引入

 

 就可以使用里面的方法了,主要是login()方法

 

 

 

有用的博客:

https://www.sohu.com/a/235364612_729271--综合实现

https://blog.csdn.net/hfutdog/article/details/82351549 --图片裁剪

分类:

技术点:

相关文章:

  • 2021-11-22
  • 2021-09-28
  • 2021-10-31
  • 2021-09-08
  • 2021-10-31
  • 2021-11-30
猜你喜欢
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2021-10-25
  • 2021-10-01
相关资源
相似解决方案