url = 'https://test.com/login'
driver.get(url)
# 获取登录页面需要输入账号密码进行模拟登录操作
user = driver.find_element(By.XPATH,'//*[@id="login"]/div[2]/div/form[2]/div[2]/div/div/input').send_keys('username')
pwd = driver.find_element(By.XPATH,'//*[@id="login"]/div[2]/div/form[2]/div[3]/div/div/input').send_keys('123456')
# 输入验证码
1.截图获取验证码图片
driver.find_element(By.XPATH,'//*[@id="login"]/div[2]/div/form[2]/div[4]/div/div/img').screenshot('verify.png')
2.将验证码图片发送给解码平台进行处理
解码平台:https://www.chaojiying.com/
第三方接口地址:http://upload.chaojiying.net/Upload/Processing.php
url_code = 'http://upload.chaojiying.net/Upload/Processing.php'
data = {
用户名
'user': 'user',
密码
'pass': '123456',
用户的个人id,注册完账号之后并且登录,然后通过个人中心申请
'sofid': '123456',
验证码的类型
'codetype': 1004
}
files = {"userfile": open("verify.png", "rb")}
resp = requests.post(url_code, data=data, files=files)
res = resp.json()
print(res)
if res['err_no'] == 0:
code = res['pic_str']
print(f"识别成功,验证码为:{code}")
else:
print("识别失败")
3.将解码后的验证码填写入验证码框中
verify = driver.find_element(By.XPATH,'//*[@id="login"]/div[2]/div/form[2]/div[4]/div/div/div/input').send_keys(code)
button = driver.find_element(By.XPATH,'//*[@id="login"]/div[2]/div/form[2]/div[5]/button').click()
sleep(5)
# 校验是否登录成功
url1 = 'https://test.com/#/serviceExpireManage'
driver.get(url1)
sleep(10)
driver.quit()