我正试图使用python的selenium包构建一个scraper,但我收到了以下错误:
Message: stale element reference: element is not attached to the page document
(Session info: headless chrome=83.0.4103.61)
我正在使用谷歌colab。我的代码是:
import selenium
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
titles=[]
for link in links:
driver.get(link)
data = driver.find_elements_by_xpath('.//a[@class = "question-hyperlink"]')
titles.append(data[0].text)
错误在data = driver.find_elements_by_xpath('.//a[@class = "question-hyperlink"]')
行
我被告知要尝试异常处理,但我无法实现它。请帮助我如何实现它。
迭代链接列表时出现同步问题。诱导WebDriverWait
((并等待visibility_of_all_elements_located
((,然后迭代并存储在列表中。
使用try。。除了要处理的块。
代码:
titles=[]
for link in links:
driver.get(link)
try:
data=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,'//a[@class = "question-hyperlink"]')))
for d in data:
titles.append(d.text)
except:
print("element not found")
continue
您需要导入以下库。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
在尝试与所需元素交互之前,您必须诱导WebDriverWait等待element_to_be_clickable()
,并且您可以使用以下定位器策略之一:
import selenium
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
titles=[]
for link in links:
driver.get(link)
data = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class = 'question-hyperlink']")))
titles.append(data[0].text)