注册流程梳理及代码封装

注册页面由邮箱、用户名、密码、验证码组成,可以进行如下封装(硬编码的封装)

#coding:utf-8
from selenium import webdriver
import time
import random
from PIL import Image
from ShowapiRequest import ShowapiRequest
driver = webdriver.Chrome()

# 浏览器初始化
def init():
    driver.get("http://www.5itest.cn/register")
    driver.maximize_windows()
    time.sleep(5)

# 获取element信息
def get_element(id):
    element = driver.find_element_by_id(id)
    return element

# 获取随机数(关于用户的随机数)
def get_range_user():
    # 进行格式化处理
    user_info = ''.join(random.sample('1234567890abcdefg',8))
    return user_info

# 获取图片
def get_code_image(file_name):
    driver.save_screenshot('file_name')
    code_element = driver.find_element_by_id('getcode_num')
    print(code_element.location)
    left = code_element.location['x']
    top = code_elemnet.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/184-4","85060","624075410c1f4901b124f1110b93b90e" )
    r.addBodyPara("img_base64", "")
    r.addBodyPara("typeId", "34")
    r.addBodyPara("convert_to_jpg", "0")
    r.addBodyPara("needMorePrecise", "0")
    r.addFilePara("imge", file_name) #文件上传时设置
    res = r.post()
    text = res.json()['showapi_res_body']['Result']
    return text

# 运行主程序
def run_main():
    user_name_info = get_range_user()
    user_mail = user_name_info + '@163.com'
    file_name = 'D:/project/Study/Image/test.png'
    driver_init()
    get_element('register_email').send_keys(user_mail)
    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)
    get_element('captcha_code').send_keys(text)
    get_element(register-btn).click()
    driver.close()

run_main()

 

posted @ 2019-09-17 14:04  pathbreaker  阅读(126)  评论(0编辑  收藏  举报