Python Selenium:我得到元素未附加到页面文档错误 [英] Python Selenium: I am getting element is not attached to the page document error

查看:43
本文介绍了Python Selenium:我得到元素未附加到页面文档错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 selenium 包为 python 构建一个刮板,但出现此错误:

I am trying to build a scraper using selenium package for python but I am getting this error:

Message: stale element reference: element is not attached to the page document
  (Session info: headless chrome=83.0.4103.61)

我正在使用 google colab.我的代码是:

I am using google colab. My code is:

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"]')

有人告诉我尝试异常处理,但我无法实施.请帮助我如何实施它.

I was told to try exception handling but I am unable to implement it. Please help me with how can I implement it.

推荐答案

迭代链接列表时似乎出现同步问题.Induce WebDriverWait() 并等待 visibility_of_all_elements_located() 然后迭代并存储在列表中.

It seems synchronization issue while iterating list of links. Induce WebDriverWait() and wait for visibility_of_all_elements_located() and then iterate and store in the list.

使用 try..except 块来处理.

Use try..except block to handle.

代码:

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

这篇关于Python Selenium:我得到元素未附加到页面文档错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆