selenium之图文验证码识别
作者:YXN-python 阅读量:105 发布日期:2023-05-10
import ddddocr
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
# 创建一个谷歌驱动器
driver = webdriver.Chrome()
# 无头浏览
# chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless')
# driver = webdriver.Chrome(options=chrome_options)
# 打开页面
driver.get("http://yixiuna.top/sgt/view/login.html")
while True:
# 使用显示等待,等待验证码出现在页面上
yzm = WebDriverWait(driver, 10).until(lambda x: x.find_element(By.XPATH, '//*[@id="yzm"]'))
# 创建文字识别对象
ocr = ddddocr.DdddOcr(show_ad=False)
# 验证码截图,进行文字识别
res = ocr.classification(yzm.screenshot_as_png)
print(f'验证码为:{res}')
r = input('如识别失败请输入n继续识别...')
if r != 'n':
break
else:
yzm.click()
driver.quit()
YXN-python
2023-05-10