簡體   English   中英

當我使用 selenium 時,我得到“StaleElementReferenceException: stale element reference: element is not attach to the page document”

[英]I get the “StaleElementReferenceException: stale element reference: element is not attached to the page document” when I use selenium

我正在嘗試使用 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=行停止工作,因為如果我刪除它,上面的任何內容都會正常工作並且瀏覽器會按預期關閉。

謝謝你的幫助。

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM