1.安装安装 ddddocr 库
pip3 install ddddocr
2.指定安装 pillow 9.5
pip3 install pillow==9.5
3.代码
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import ddddocr
ocr = ddddocr.DdddOcr()
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging']) # for ignore warning and error
driver = webdriver.Chrome(options=options)
driver.implicitly_wait(4)
driver.get('http://127.0.0.1/1.html')
time.sleep(2) # 这里出现captcha时间有点长,等待2秒
while True:
# 获取元素展示内容为图片数据
pngData = driver.find_element(By.ID,'captcha').screenshot_as_png
# with open('d:/tmp1.png', 'wb') as f:
# f.write(pngData)
res = ocr.classification(pngData)
print('验证码是', res)
ch = input('')
if ch != '':
break
driver.refresh()
B站链接:Selenium 自动化 验证码输入_哔哩哔哩_bilibili
网址:www.byhy.net