首页 > 文章列表 > Python for循环中第二次定位不到元素问题:为什么我的代码无法找到元素?

Python for循环中第二次定位不到元素问题:为什么我的代码无法找到元素?

418 2025-02-01

Python for循环中第二次定位不到元素问题:为什么我的代码无法找到元素?

python for循环中,第二次定位不到元素问题

在进行python登录参数化测试时,出现了for循环第二遍执行报错,无法定位元素的问题。

此问题是由以下原因引起的:

解决方案:

将浏览器的调用放置在for循环内,即可解决此问题。

import unittest
import time
import xlrd
from selenium import webdriver

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()
来源:1730204830