以配置文件形式实现定位设计思想>读取配置文件>封装定位元素类>模块化注册页面实践
Python中配置文件的后缀建议都使用以“.ini”结尾的
首先在项目目录中新增一个config的目录,在该目录下新增一个LocalElement.ini
LocalElement.ini可用于存储元素信息、数据库信息.......,信息大致如下
[RegisterElement] user_email=id>register_email user_name=id>register_nickname password=id>register_password code_image=id>getcode_num code_text=id>captcha_code
register_button=id>register-btn
读取配置文件
读取配置文件需要使用第三方拓展包“Configparser”,可利用 pip install Configparser
在项目目录下创建read_ini.py
#coding=utf-8 import configparser cf = configparser.ConfigParser() # 读取配置文件 cf.read('D:/test/config/LocalElement.ini') print(cf.get('RegisterElement', 'user_email'))
优化read_ini.py(重构封装读取配置文件方法)
#coding=utf-8 import configparser class ReadIni(object): def __init__(self,file_name=None,node=None): # 如果file_name为None时,进行默认赋值 if file_name == None: file_name = "D:/test/config/LocalElement.ini" # 如果ini文件中的section为None时,默认赋值 if node == None: self.node = 'RegisterElement' # 否则就引用初始化时传入的值 else: self.node = node self.cf = self.load_ini(file_name) # 加载文件 def load_ini(self, file_name): cf = configparser.ConfigParser() # 读取配置文件 cf.read(file_name) return cf # 获取配置文件中的值 def get_value(self, key): data = self.cf.get(self.node,key) return data if __name__ == '__main__': # 初始化对象 read_init = ReadIni() print(read_init.get_value('user_name'))
设计封装定位元素类
项目目录下新增一个find_element.py文件,用于封装定位元素类
#coding:utf-8 from read_ini import ReadIni class FindElement(object): def __init__(self,driver): self.driver = driver def get_element(self,key) read_ini = ReadIni() data = read_ini.get_value(key) # 拆分元素返回值 by = data.split('>')[0] value = data.split('>')[1] try: if by == 'id': return self.driver.find_element_by_id(value) elif by == 'name': return self.driver.find_element_by_name(value) elif by == 'className': return self.driver.find_element_by_className(value) else: return self.driver.find_element_by_xpath(value) except: return None
将整个注册流程脚本进行模块化
#coding=utf-8 from selenium import webdriver import time import random import PIL import Image from ShowapiRequest import ShowapiRequest from find_element import FindElement class RegisterFuntion(object): def __init__(self,url): self.driver = self.get_driver(url) # 获取driver并且打开ul def get_driver(self,url): driver = webdriver.Chrome() driver.get(url) driver.maxizime_window() return driver # 输入用户信息 def send_user_info(self,key,data): self.get_user_element(key).send_keys(data) #定位用户信息,获取element def get_user_element(self,key): find_element=FindElement(self.driver) user_element = find_element.get_element(key) return user_element # 获取随机数(关于用户的随机数) def get_range_user(self): # 进行格式化处理 user_info = ''.join(random.sample('1234567890abcdefg',8)) return user_info # 获取图片 def get_code_image(self,file_name): self.driver.save_screenshot('file_name') code_element = self.driver.get_user_element('code_image') 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(self,file_name): self.get_code_image(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 main(): user_name_info = self.get_range_user() user_mail = user_name_info + '@163.com' file_name = 'D:/project/Study/Image/test.png' code_text = self.code_online(file_name) self.send_user_info('user_email',user_email) self.send_user_info('user_name',user_name_info) self.send_user_info('password','111111') self.send_user_info('code_text',code_text) self.get_user_element('register_button').click() time.sleep(5) self.driver.close()
if __name__ == '__main__':
register_funtion = RegisterFuntion('http://www.5itest.cn/register')
register_funtion.main()
get_value