selenium报错:Message: stale element reference: element is not attached to the page documentselenium报错:Message: stale element reference: element is not attached to the page documentselenium报错:Message: stale element reference: element is not attached to the page documentselenium报错:Message: stale element reference: element is not attached to the page document
  • 首页
  • 博客
  • 书签
  • 文件
  • 分析
  • 登录

selenium报错:Message: stale element reference: element is not attached to the page document

Published by admin at 2022年3月15日
Categories
  • Python
Tags

报错的原因:

所引用的元素已过时,不再依附于当前页面。通常情况下,这是因为页面进行了刷新或跳转。在先获取xpath列表,再引用列表时,容易出现此问题。

解决方法:

重新定位元素

代码示例:

# 旧代码(报错)
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))
# 新代码(解决方法)
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()

 

发表回复 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Categories

  • Cat
  • Python
  • MySQL
  • Django
  • Html/CSS
  • JavaScript
  • Vue
  • RegExp
  • php
  • Practice
  • Virtualization
  • Linux
  • Windows
  • Android
  • NAS
  • Software
  • Hardware
  • Network
  • Router
  • Office
  • WordPress
  • SEO
  • English
  • Games
  • Recipes
  • living
  • Memorandum
  • Essays
  • 未分类

归档

©2015-2022 Alaica Blog support@alaica.com
      ajax-loader