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 = \'*******/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