參數(shù)化的方式:1)讀取txt文件
? ? ? ? ? ? ? ? ? ? ? ? ?2)函數(shù)
? ? ? ? ? ? ? ? ? ? ? ? ?3)字典
? ? ? ? ? ? ? ? ? ? ? ? ?4)讀取csv文件
1拢肆、讀取txt文件
將用戶名和密碼分別存儲(chǔ)在一個(gè)txt文件中冶伞,通過讀取txt文件獲取用戶名和密碼
# 讀取用戶名
my_file1=open("username.txt")
data=my_file.read()
my_file1.close()
# 讀取密碼
my_file1=open("username.txt")
data=my_file.read()
my_file1.close()
分別打開兩個(gè)txt 文件凳鬓,通過un 和pw 來接收用戶名和密碼信息袋哼,將接收的數(shù)據(jù)通過send_key(xx)轉(zhuǎn)入到執(zhí)行程序中遍略。實(shí)現(xiàn)了數(shù)據(jù)與腳本分離的目的
缺點(diǎn):
1浅悉、用戶名密碼分別在不同的文件里闽颇,修改用戶名和密碼比較麻煩胸嘴。
2雏掠、username.txt 和password.txt 文件中只能保存一個(gè)用戶密碼,無能很好的循環(huán)讀取劣像。
2乡话、登錄參數(shù)化(函數(shù))
def data(un="xxx",pw="xxx"):
? ? ? ? ? ? ? print("success")
? ? ? ? ? ? ? return username,password)
un,pw=data()
3、登錄參數(shù)化(字典)
def dictionary():
? ? ? ? ?dic={"username":"xxx","password":"xxx"}
? ? ? ? ?return dic
dic=dictionary()
4耳奕、表單參數(shù)化
通過WPS 或excel 創(chuàng)建表格绑青,文件另存為選擇CSV 格式
csv.reader()用于讀取CSV 文件,user[0] 表示表格中第一行第一列的數(shù)據(jù)(用戶名)屋群,user[1]表示表格中第一行第二列的數(shù)據(jù)(郵箱)闸婴,后面類推。通過CSV 讀取文件比較靈活芍躏,可以循環(huán)讀取每一條數(shù)據(jù)掠拳,從而又不局限每次所讀取數(shù)據(jù)的個(gè)數(shù)。
腳本的模塊化與參數(shù)化是我們?cè)谧詣?dòng)化腳本開發(fā)中用到最多的兩個(gè)技巧纸肉,工作中要多總結(jié)溺欧。