新建一个配置文件 LocalElement.ini
[RegedElement]
user_mail=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
----------------------------------------------------------------------------------
新建一个test4文件
#coding=utf-8
import configparser
class RedIni(object):
def __init__(self,fil_name=None,node=None):
if fil_name==None:
fil_name=r"D:\Users\13413\PycharmProjects\untitled\selenium自动化\LocalElement.ini"
if node==None:
self.code="RegedElement"
else:
self.node=node
self.cf=self.load_ini(fil_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.code,key)
return data
if __name__=='__main__':
read_init=RedIni()
print(read_init.get_value('user_name'))
------------------------------------------------------------------------------------------
新建一个包带有__init__文件 find_element文件
#coding=utf-8
from util.test4 import RedIni
class FindElement(object):
def __init__(self,driver):
self.driver=driver
def get_element(self,key):
read_ini=RedIni()
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_class_name(value)
else:
return self.driver.find_element_by_xpath(value)
except:
return None
-----------------------------------------------------------------------------------------
新建一个test5文件
#coding=utf-8
from selenium import webdriver
import time
import random
from PIL import Image
from ShowapiRequest import ShowapiRequest
from util.find_element import FindElement
class RegedisterFunction(object):
def __init__(self,url):
self.driver = self.get_driver(url)
def get_driver(self,url):
driver=webdriver.Chrome()
driver.get(url)
driver.maximize_window()
return driver
#输入用户信息
def send_user_info(self,key,data):
self.get_user_element(key).send_keys(data)
#定位用户信息
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('123456789abcdefg', 8))
return user_info
#获取图片
def get_code_image(self,file_name):
self.driver.save_screenshot(file_name)
code_element = self.get_user_element("code_image")
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)
new_image = im.resize((674, 527), Image.BILINEAR)
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", "62626", "d61950be50dc4dbd9969f741b8e730f5")
r.addBodyPara("typeId", "35")
r.addBodyPara("convert_to_jpg", "0")
r.addFilePara("image", file_name) # 文件上传时设置
res = r.post()
text = res.json()['showapi_res_body']['Result']
print(text) # 返回信息
return text
def main(self):
user_name_info=self.get_range_user()
user_email=user_name_info+"@163.com"
file_name="E:/tupian/12345.png"
code_text=self.code_online(file_name)
self.send_user_info("user_mail",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_function= RegedisterFunction("http://www.5itest.cn/register")
register_function.main()