import os
from datetime import datetime
from time import sleep

import ddddocr
from PIL import Image
from selenium import webdriver
from selenium.webdriver.common.by import By

import time

driver = webdriver.Chrome()
driver.maximize_window()
uri = driver.get('XXXXX')
driver.find_element(By.ID,'userName').send_keys('xxxxx')
driver.find_element(By.ID,'password').send_keys('xxxxx')

sys_time = time.strftime('%Y%m%d%H%M%S')
png_name = sys_time + "code" + ".png"
dir_path = os.path.dirname(__file__)
png_path = os.path.join(dir_path,png_name)
driver.get_screenshot_as_file(png_path)

#通过定位验证码元素来截取验证码截图
code_element = driver.find_element(By.XPATH,"//*[contains(@id,'validCode')]/img")
left = code_element.location['x']
top = code_element.location['y']
right = code_element.size['width']+left
height = code_element.size['height']+top
im = Image.open(png_path)
截取图片验证码
img = im.crop((left, top, right, height))
截取的验证码图片保存为新的文件
img.save(png_path)
ocr = ddddocr.DdddOcr()
with open(png_path,'rb') as f:
imag = f.read()
res = ocr.classification(imag)
print(res)
driver.find_element(By.XPATH,"//*[@class='container']/div[4]/input").send_keys(res)
driver.find_element(By.XPATH,"//*[@class='container']/input").click()
sleep(2)
driver.close()
posted on 2022-06-15 21:44  安好☞☞☞  阅读(299)  评论(0编辑  收藏  举报