我们所关注的数据参数化
数据参数化(TXT/EXCEL)
1.txt数据参数化(以百度搜索为例)
#coding=utf-8import time,os
from selenium.webdriver.common.by import By
from selenium import webdriver
'''
1.先找到并打开txt文档
2.使用readlines一行行去读,直到文档结束
3.使用For循环遍历
'''
#打开目标文档
source = open("C:\\test.txt","r","utf-8")
values = source.readlines()
#遍历txt文档中的内容
for search in values:
driver = webdriver.Firefox()
driver.get("https://www.baidu.com/")
driver.find_element(By.ID,"kw").send_keys(search)
time.sleep(3)
driver.find_element(By.ID,"su").click()
driver.quit()
PS:感兴趣的可以自己动手试一下登录(别试百度,有验证码)
2.EXCEL数据参数化
表格内容如下:
(1)去官网下载xlrd库:http://pypi.python.org/pypi/xlrd
# coding:utf-8
from selenium import webdriver
import xlrd,time
def open_excel(file='file.xls'):
try:
data = xlrd.open_workbook(file)
return data
except Exception, e:
print str(e)
def excel_table_byindex(file='file.xls', colnameindex=0, by_index=0):
data = open_excel(file)
table = data.sheets()[by_index]
nrows = table.nrows# 行数
colnames = table.row_values(colnameindex)# 某一行数据
list = []
for rownum in range(1, nrows):
row = table.row_values(rownum)
if row:
app = {}
for i in range(len(colnames)):
app[colnames[i]] = row[i]
list.append(app)
return list
def login():
listdata = excel_table_byindex("C:\\data.xlsx", 0)
if (len(listdata) <= 0):
assert 0, u"数据错误,请检查excel"
for i in range(0, len(listdata)):
browser = webdriver.Firefox()
browser.get(u"目标地址")
'''
例如公司网站的登录,自行添加代码'''
if __name__ == '__main__':
login()
欢迎关注‘我要学测试’微信公众号: