首页 新闻 会员 周边 捐助

python for循环,第二遍定位不到元素?

-1
悬赏园豆:5 [已解决问题] 解决于 2018-05-31 11:25

python读取excel数据,登录参数化,for循环测试

第一遍执行成功,第二遍报错,无法定位

작은악마的主页 작은악마 | 菜鸟二级 | 园豆:212
提问于:2018-05-23 13:03
< >
分享
最佳答案
0

调用浏览器放在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

작은악마 | 菜鸟二级 |园豆:212 | 2018-05-31 11:24
其他回答(1)
1

第二遍的时候找不到页面id元素  看下页面是否变化了 需要切换到新页面再定位

收获园豆:5
ycyzharry | 园豆:25683 (高人七级) | 2018-05-23 14:21

第一遍执行完退出浏览器,第二遍浏览器都没调出来就报错了

支持(0) 反对(0) 작은악마 | 园豆:212 (菜鸟二级) | 2018-05-23 19:53

@夏天的风i: 不要退出driver

支持(0) 反对(0) ycyzharry | 园豆:25683 (高人七级) | 2018-05-23 19:56

@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

 

支持(0) 反对(0) 작은악마 | 园豆:212 (菜鸟二级) | 2018-05-24 09:03

@夏天的风i: 你先不加变量i,赋值i=1,i=2调试2遍差不多就可以定位原因了。

支持(0) 反对(0) ycyzharry | 园豆:25683 (高人七级) | 2018-05-24 09:16

@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

 

支持(0) 反对(0) 작은악마 | 园豆:212 (菜鸟二级) | 2018-05-24 20:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册