当我使用selenium时,我得到“StaleElementReferenceException:stale元素引用:元素未附加到页面文档”

2024-03-29 09:25:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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( " ", @class, " " ), 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=行停止工作,因为如果我删除它,上面的任何内容都会工作,浏览器会按预期关闭

谢谢你的帮助