testdata.xlsx
脚本:
from selenium import webdriver
import time
import datetime
from openpyxl import *
wb=load_workbook(r"D:TestFrameTestDate estdata.xlsx")
#获取第一个sheet
ws = wb.active
#打印最大的行号
print("最大行号是:",ws.max_row)
#excel行是从第1行开始的,所以我们要从第2行开始迭代遍历
#使用切片的时候,必须有结束行的索引号,不能写成[1:]
#列号是从0开始,列取出来是个元组
#打开浏览器
driver = webdriver.Chrome("e:\chromedriver.exe")
for row in ws[2:ws.max_row]:
print(row[1])
print(row[2])
try:
driver.get("http://www.baidu.com")
driver.find_element_by_id("kw").send_keys(row[1].value)
driver.find_element_by_id("su").click()
time.sleep(3)
assert row[2].value in driver.page_source
row[3].value = time.strftime("%Y-%m-%d %H:%M:%S")
row[4].value = "断言成功"
except AssertionError as e:
row[3].value =time.strftime("%Y-%m-%d %H:%M:%S")
row[4].value = "断言失败"
except AssertionError as e:
row[3].value = time.strftime("%Y-%m-%d %H:%M:%S")
row[4].value = "出现异常"
driver.quit()
wb.save(r"D:TestFrameTestDate estdata.xlsx") #注意是覆盖不是更新
执行结果:
D:pythonpython.exe D:/TestFrame/ff.py
最大行号是: 4
<Cell 'Sheet1'.B2>
<Cell 'Sheet1'.C2>
<Cell 'Sheet1'.B3>
<Cell 'Sheet1'.C3>
<Cell 'Sheet1'.B4>
<Cell 'Sheet1'.C4>
Process finished with exit code 0