目录
前言:selenium自动化测试时,使用循环去遍历时报错
一、原因
所引用的元素已过时,不再依附于当前页面。通常情况下,这是因为页面进行了刷新或跳转
二、解决方法
重新点位元素
三、代码例子
1、问题代码
# 旧代码(报错)
lists = self.root.find_elements_by_xpath('/html/body/table[4]/tbody/tr[2]/td/a')
for i in lists:
self.root.get(i.get_attribute(''href))
2、修复代码
# 新代码(解决方法)
lists = self.root.find_elements_by_xpath('/html/body/table[4]/tbody/tr[2]/td/a')
for j in range(len(lists)):
self.root.find_elements_by_xpath('/html/body/table[4]/tbody/tr[2]/td/a')[j].click()