Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

python - I get the "StaleElementReferenceException: stale element reference: element is not attached to the page document" when I use selenium

I'm trying to scrape the table from following website using selenium: https://web.archive.org/web/20120220031809/http://simcentral.net/ibaf/games/1

with the code:

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()

I get the following error:

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

I looked online and understand the problem, but don't know what to do about it. The other issues appear to be pulling the elements via some way other than through xpath. I know it stops working at the table_html= line because if I remove it, whatever is above works and the browser closes as expected.

Thanks for any help.

question from:https://stackoverflow.com/questions/65941400/i-get-the-staleelementreferenceexception-stale-element-reference-element-is-n

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...