直接上代码:
import base64
import ddddocr
import requests
import jsonpath
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.chrome.webdriver import Options
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
def openurl():
driver = webdriver.Chrome(options=chrome_options)
driver.get(url='http://localhost/#/login')
driver.find_element(By.NAME, "username").send_keys("admin")
driver.find_element(By.NAME, "password").send_keys("123456")
# 获取验证码元素
ele_img = driver.find_element(By.XPATH,
"//div[@id='app']/div/div/div/form/div[5]/div/div[2]/img")
#截图
ele_img.screenshot('dd.png')
with open('dd.png', 'rb') as f:
img = f.read()
ocr = ddddocr.DdddOcr(show_ad=False)
#识别
code = ocr.classification(img)
print(code)
#验证码输入框赋值
driver.find_element(By.CSS_SELECTOR, ".el-form-item:nth-child(6) .el-input__inner").send_keys(code)
#点击登陆
driver.find_element(By.CSS_SELECTOR, ".el-button").click()
# def get_verifycode():
# resp = requests.get('http://localhost:8800/ae')
# json_data = resp.json()
# # code = jsonpath.jsonpath(json_data, '$.code')[0]
# img = jsonpath.jsonpath(json_data, '$.data.img')[0]
# uuid = jsonpath.jsonpath(json_data, '$.data.uuid')[0]
# # print(code)
# # print(img)
# # print(uuid)
# img111 = base64.b64decode(img)
# with open('cap.png', 'wb') as f:
# f.write(img111)
# ocr = ddddocr.DdddOcr(show_ad=False)
# code = ocr.classification(img111)
# return code
if __name__ == '__main__':
openurl()
# get_verifycode(self=None)
直接运行即可。