webinfo.txt
url=http://www.maiziedu.com/
text_id=登录
userid=id_account_l
pwdid=id_password_l
loginid=login_btn
userinfo.txt
account=18665933615 pwd=abc123456
account=123 pwd=abc123456
account=632345244@qq.com pwd=z5
account=632345244@qq.com pwd=123456
account= pwd=abc123456
account=18665933615 pwd=
userdata.py
def get_webinfo():
web_info = {}
f = open(r'E:\project\demo\mokuaihua\webinfo.txt','r',encoding='utf-8')
infos = f.readlines()
f.close()
for info in infos:
info = info.strip()
# print(info)
web_info[info.split('=')[0]] = info.split('=')[1]
return web_info
def get_userinfo():
user_list = []
#user_info = {}
f = open(r'E:\project\demo\mokuaihua\userinfo.txt', 'r', encoding='utf-8')
infos = f.readlines()
f.close()
print(infos)
for info in infos:
user_info = {}
info = info.strip().split(' ')
user_info[info[0].split('=')[0]] = info[0].split('=')[1]
user_info[info[1].split('=')[0]] = info[1].split('=')[1]
user_list.append(user_info)
#print(user_list)
return user_list
if __name__ == '__main__':
webinfo = get_webinfo()
print(webinfo)
userinfo = get_userinfo()
print(userinfo)
用例
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
#从文件中导入数据
from userdata import get_webinfo,get_userinfo
def waituntil_load(driver,times,func):
return WebDriverWait(driver,times).until(func)
def openBrower():
driver = webdriver.Chrome()
return driver
def openUrl(driver,url):
driver.get(url)
driver.maximize_window()
def findElement(driver,arg):#参数是字点
# driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[3]/div/div').click() #关闭弹窗
waituntil_load(driver, 15, lambda driver: driver.find_element_by_xpath('/html/body/div[3]/div[2]/div[3]/div/div')).click()
if 'text_id' in arg.keys():
element_login = waituntil_load(driver,15,lambda driver:driver.find_element_by_link_text(arg['text_id']))
element_login.click()
time.sleep(3)
userEle = driver.find_element_by_id(arg['userid'])
pwdEle = driver.find_element_by_id(arg['pwdid'])
loginEle = driver.find_element_by_id(arg['loginid'])
return userEle,pwdEle,loginEle
def sendValues(eletuple,arg):#arg为登录数据,格式为字典
listkey = ['account','pwd']
i = 0
for key in listkey:
eletuple[i].clear()
eletuple[i].send_keys(arg[key])
i += 1
eletuple[-1].click()
def checkResult(driver,i):
try:
a = driver.find_element_by_id('login-form-tips')
print('第%d个账号登录失败,提示信息为:%s'%(i+1,a.text))
except:
print('第%d个账号登陆成功'%(i+1))
def login_test(ele_dict,user_list):
driver = openBrower()
openUrl(driver,ele_dict['url'])
ele_tuple = findElement(driver,ele_dict)
for i in range(len(user_list)):
sendValues(ele_tuple,user_list[i])
time.sleep(3)
checkResult(driver,i)
time.sleep(3)
if __name__ == '__main__':
'''
ele_dict = {'url': 'http://www.maiziedu.com/',
'text_id': '登录',
'userid': 'id_account_l',
'pwdid': 'id_password_l',
'loginid': 'login_btn',
#'account' : '18665933615',
#'pwd' : 'abc123456',
#'errorinfo' : '账号或者密码错误,请重新输入'
}
user_list = [{'account' : '18665933615','pwd' : 'abc123456'},
{'account': '123', 'pwd': 'abc123456'},
{'account' : '632345244@qq.com','pwd' : 'zu615'}]'''
ele_dict = get_webinfo()
print(ele_dict)
user_list = get_userinfo()
print(user_list)
login_test(ele_dict,user_list)
第1个账号登录失败,提示信息为:账号或者密码错误,请重新输入
第2个账号登录失败,提示信息为:该账号格式不正确
第3个账号登陆成功