开发环境:win10 + python3.7 + selenium + ddddocr + PIL
说明:这里测试的是4位数字验证码的情况,理论上加上字母应该也是可以的
话不多说,上代码样例:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
import ddddocr
from PIL import Image
import os
def getToken():
url = "http://localhost/#/login"
from selenium.webdriver.firefox.options import Options
firefox_options = Options()
firefox_options.headless = False
driver = webdriver.Firefox(options=firefox_options)
# 设置等待时间
waite = WebDriverWait(driver, 5)
driver.get(url)
driver.implicitly_wait(6)
# 1.先保存当前页面为一个png图
driver.get_screenshot_as_file("page.png")
inputname = driver.find_elements_by_class_name("el-input__inner")[0]
inputname.click()
inputname.send_keys("10000")
inputpassword = driver.find_elements_by_class_name("el-input__inner")[1]
inputpassword.send_keys("123456")
# choice = driver.find_element_by_class_name("person")
choice = driver.find_element_by_xpath('/html/body/div/div/div/div[1]/div/div[2]/div/div[2]/div[2]')
choice.click()
# 2.然后将页面截图中的验证码截取出来
image = Image.open("page.png")
captchaurl = driver.find_element_by_class_name("login-code-img")
left = captchaurl.location.get("x")
top = captchaurl.location.get("y")
right = left + captchaurl.size.get("width")
bottom = top + captchaurl.size.get("height")
cropImg = image.crop((left, top, right, bottom))
cropImg.save("code.png")
# 3.读取验证码图
with open('code.png','rb') as f:
content_captcha = f.read()
# print(content_captcha)
# 4.此处是重点,解析验证码,得到4位数字,只需要2行代码
ocr = ddddocr.DdddOcr()
code = ocr.classification(content_captcha)
print(code)
inputcaptcha = driver.find_elements_by_class_name("el-input__inner")[2]
inputcaptcha.send_keys(code)
button = driver.find_element_by_tag_name("button")
button.click()
# logs = driver.get_log('performance')
cookie = driver.get_cookies()
print(cookie)
if cookie != []:
token = cookie[0]['value']
else:
token = ""
driver.quit()
return token
token = ""
result = False
while result == False:
token = getToken()
if token != "":
result = True
print(token)