调用浏览器放在for下面解决:
import unittest
import time
import xlrd
from selenium import webdriver
def open_excel(file='file.xls'):
try:
data = xlrd.open_workbook(file)
return data
except Exception as 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
class login(unittest.TestCase):
def test(self):
tabls = excel_table_byindex(file='./data/meit.xlsx')
print(tabls)
if (len(tabls) <= 0):
assert 0, u"数据异常"
for i in range(0, len(tabls)):
self.dr = webdriver.Firefox()
self.dr.maximize_window()
self.dr.implicitly_wait(10)
self.dr.get("https://passport.meituan.com/account/unitivelogin?service=www&continue=http%3A%2F%2Fwww.meituan.com%2Faccount%2Fsettoken%3Fcontinue%3Dhttps%253A%252F%252Fwww.meituan.com%252F")
time.sleep(5)
print(i)
print(tabls[i]['username'])
print(tabls[i]['password'])
# 登录
self.dr.find_element_by_id('login-email').send_keys(tabls[i]['username'])
self.dr.find_element_by_id("login-password").send_keys(tabls[i]['password'])
self.dr.find_element_by_class_name('btn').click()
time.sleep(3)
self.dr.close()
def tearDown(self):
time.sleep(3)
self.dr.quit()
if __name__ == '__main__':
unittest.main
第二遍的时候找不到页面id元素 看下页面是否变化了 需要切换到新页面再定位
第一遍执行完退出浏览器,第二遍浏览器都没调出来就报错了
@夏天的风i: 不要退出driver
@ycyzharry: 也不行,我的代码
import unittest
import time
import xlrd
from selenium import webdriver
import selenium
def open_excel(file='file.xls'):
try:
data = xlrd.open_workbook(file)
return data
except Exception as 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
class login(unittest.TestCase):
def setUp(self):
self.dr = webdriver.Firefox()
self.dr.maximize_window()
self.dr.implicitly_wait(10)
self.dr.get("http://*******/")
time.sleep(5)
def test(self):
tabls = excel_table_byindex(file='./data/01.xlsx')
print(tabls)
if (len(tabls) <= 0):
assert 0, u"数据异常"
for i in range(0, len(tabls)):
print(tabls[i]['username'])
print(tabls[i]['password'])
# 登录
self.dr.find_element_by_id('loginName').send_keys(tabls[i]['username'])
self.dr.find_element_by_id("loginPass").send_keys(tabls[i]['password'])
self.dr.find_element_by_class_name('btn').click()
time.sleep(3)
link = self.dr.find_element_by_id("navDivContainer").text
self.assertEqual(link, tabls[i]['navDivContainer'])
print(link)
self.dr.refresh()
# def tearDown(self):
# time.sleep(3)
# self.dr.quit()
if __name__ == '__main__':
unittest.main
循环打印出来了值,就是循环到self.dr.find_element_by_id('loginName').send_keys(tabls[i]['username']) ,报错找不到id=loginName
@夏天的风i: 你先不加变量i,赋值i=1,i=2调试2遍差不多就可以定位原因了。
@ycyzharry: 这个是美团的代码,大神请赐教一下,谢了
import unittest
import time
import xlrd
from selenium import webdriver
def open_excel(file='file.xls'):
try:
data = xlrd.open_workbook(file)
return data
except Exception as 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
class login(unittest.TestCase):
def setUp(self):
self.dr = webdriver.Firefox()
self.dr.maximize_window()
self.dr.implicitly_wait(10)
self.dr.get("https://passport.meituan.com/account/unitivelogin?service=www&continue=http%3A%2F%2Fwww.meituan.com%2Faccount%2Fsettoken%3Fcontinue%3Dhttps%253A%252F%252Fwww.meituan.com%252F")
time.sleep(5)
def test(self):
tabls = excel_table_byindex(file='./data/meit.xlsx')
print(tabls)
if (len(tabls) <= 0):
assert 0, u"数据异常"
for i in range(0, len(tabls)):
print(i)
print(tabls[i]['username'])
print(tabls[i]['password'])
# 登录
self.dr.find_element_by_id('login-email').send_keys(tabls[i]['username'])
self.dr.find_element_by_id("login-password").send_keys(tabls[i]['password'])
self.dr.find_element_by_class_name('btn').click()
time.sleep(3)
self.dr.close()
def tearDown(self):
time.sleep(3)
self.dr.quit()
if __name__ == '__main__':
unittest.main