#encoding=utf-8
from selenium import webdriver
from PIL import Image
from ShowapiRequest import ShowapiRequest
import time
import random
driver=webdriver.Chrome()
#浏览器初始化
def driver_init():
driver.get("http://www.5itest.cn/register")
driver.maximize_window()
time.sleep(5)
# 获取元素
def get_element(id):
return driver.find_element_by_id(id)
# 获取随机数
def get_range_user():
user_info = "".join(random.sample("1234567890abcdefhigklmnopkrstuvwxyz",8))
return user_info
# 获取图片
def get_code_image(file_name):
driver.save_screenshot(file_name)
code_element=driver.find_element_by_id("captcha_code")
left=code_element.location["x"]
top=code_element.location["y"]
right=code_element.size["width"]+left
height=code_element.size["height"]+top
im=Image.open(file_name)
img=im.crop((left,top,right,height))
img.save(file_name)
#解析图片,获取验证码
def code_online(file_name):
r = ShowapiRequest("http://route.showapi.com/932-2", "106571",
"3606ba9b88794da7a78bf35d4545f54a") # appid,appsecret需个人中心申请修改
r.addBodyPara("typeId", "35")
r.addBodyPara("convert_to_jpg", "0")
r.addFilePara("image", file_name) # 文件上传时设置
res = r.post()
print(res.text)
text=res.json()['showapi_res_body']['image']
return text
# 运行主程序
def run_main():
user_name_info=get_range_user()
user_eamil=user_name_info+"@163.com"
file_name="E:/Program/Test/01.png"
# 浏览器初始化
driver_init()
# 获取元素
get_element("register_email").send_keys(user_eamil)
get_element("register_nickname").send_keys(user_name_info)
get_element("register_password").send_keys(111111)
get_code_image(file_name)
text=code_online(file_name)
driver.find_element_by_id("captcha_code").send_keys(text)
driver.find_element_by_id("register-btn").click()
driver.close()
run_main()