程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用 selenium 时出现“StaleElementReferenceException: stale element reference: element is not attach to the page document”大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用 selenium 时出现“StaleElementReferenceException: stale element reference: element is not attach to the page document”?

开发过程中遇到使用 selenium 时出现“StaleElementReferenceException: stale element reference: element is not attach to the page document”的问题如何解决?下面主要结合日常开发的经验,给出你关于使用 selenium 时出现“StaleElementReferenceException: stale element reference: element is not attach to the page document”的解决方法建议,希望对你解决使用 selenium 时出现“StaleElementReferenceException: stale element reference: element is not attach to the page document”有所启发或帮助;

我正在尝试使用 selenium 从以下网站抓取表格: https://web.archive.org/web/20120220031809/http://simcentral.net/ibaf/games/1

代码:

from selenium import webdriver as wd
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import webdriverwait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup as bs
from pandas.io.HTML import read_HTML
import pandas as pd
import numpy as np
import re
os.chdir('c:/Users/Owner')
bat=pd.DataFrame()

driver = wd.Chrome()
wait = webdriverwait(driver,15)
driver.get('https://web.archive.org/web/20120220031809/http://simcentral.net/ibaf/games/1')
page=driver.find_element_by_xpath('//*[contains(concat( " ",@class," " ),concat( " ","regtext"," " ))] | //*[contains(concat( " ","normal"," " ))]')
table_HTML=page.get_attribute('INNERHTML')

driver.quit()

我收到以下错误:

StaleElementReferenceException: stale element reference: element is not attached to the page document

我在网上查看并了解问题,但不知道该怎么办。其他问题似乎是通过 xpath 以外的其他方式拉动元素。我知道它在 table_HTML= 行停止工作,因为如果我删除它,上面的任何内容都可以正常工作并且浏览器会按预期关闭。

感谢您的帮助。

解决方法

试试这个

import pandas as pd
data = pd.read_html("https://web.archive.org/web/20120220031809/http://simcentral.net/ibaf/games/1")

大佬总结

以上是大佬教程为你收集整理的使用 selenium 时出现“StaleElementReferenceException: stale element reference: element is not attach to the page document”全部内容,希望文章能够帮你解决使用 selenium 时出现“StaleElementReferenceException: stale element reference: element is not attach to the page document”所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: