WebUI自动化测试验证码登录如何实现?
点击查看代码
# encoding=utf8
import sys
import requests
import muggle_ocr
import json
class loginerp():
def __init__(self):
# 将request 开启会话
self.session=requests.session()
# 登录获取cookie
def yanzhengma(self):
img_code_url = "http://192.168.1.235/system/index.php/admin/managers/imagenumber?1662719683" # 验证码图片地址
png = self.session.get(url=img_code_url) # 请求验证码
# 将验证码保存本地
filename = "D:\pycharmproject\验证码识别\cap.png" # 定义一个图片地址
file = open(filename, "wb") # 以二进制打开一个文件
file.write(png.content) # 写入二进制文件
file.close() # 关闭文件
# 调用muggle_ocr
sdk = muggle_ocr.SDK(model_type=muggle_ocr.ModelType.Captcha)
with open(r"D:\pycharmproject\验证码识别\cap.png", "rb") as f: # 打开文件
img = f.read() # 读取图片文件
text = sdk.predict(image_bytes=img) # 识别验证码
print(text) # 打印下验证码
return text
def erp_login(self):
while True:
text=self.yanzhengma()
# 定义请求登录地址
url="http://192.168.1.235/system/index.php/admin/managers/login"
#定义请求头
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
}
#请求请求登录的参数
data={
"username":"wengzhenxiong",
"password":"wenzhenxiong12",
"number":text, #取值为muggle_ocr 识别的验证码数字
"action":"login"
}
aa=self.session.post(url=url,headers=headers,data=data).text
if "首页" in aa: #判断是否有首页 有则登录成功
print(111)
return aa
def get_cookiejar(self):
self.erp_login()
cookies_jar=self.session.cookies
print(cookies_jar)
# cookie = requests.utils.dict_from_cookiejar(cookie_jar)
cookies=cookies_jar.get_dict()
print(cookies)
cookie=cookies['PHPSESSID']
print(cookie)
return cookie