简体   繁体   English

Python Selenium:我正在获取元素未附加到页面文档错误

[英]Python Selenium: I am getting element is not attached to the page document error

I am trying to build a scraper using selenium package for python but I am getting this error:我正在尝试使用 selenium package 为 python 构建刮板,但我收到此错误:

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

I am using google colab.我正在使用谷歌 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)

The error is in line data = driver.find_elements_by_xpath('.//a[@class = "question-hyperlink"]')错误在行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.请帮助我如何实现它。

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.诱导WebDriverWait (),等待visibility_of_all_elements_located (),然后迭代存储到列表中。

Use try..except block to handle.使用 try..except 块来处理。

Code :代码

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

You need to import below libraries.您需要导入以下库。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Before you attempt to interact with the desired elements you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategy :在您尝试与所需元素进行交互之前,您必须为element_to_be_clickable()诱导WebDriverWait并且您可以使用以下任一Locator Strategy

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python selenium 错误:元素未附加到页面文档 - python selenium error: element is not attached to the page document python selenium错误陈旧元素:元素未附加到页面文档 - python selenium error stale element: element is not attached to the page document 我收到此错误“消息:过时的元素引用:元素未附加到页面文档” - I am getting this error "Message: stale element reference: element is not attached to the page document" 收到错误“消息:过时的元素参考:元素未附加到页面文档”。我做错了什么 - Getting error "Message: stale element reference: element is not attached to the page document".what i am doing wrong Python Selenium:元素未附加到页面文档错误,行为异常 - Python Selenium: element is not attached to the page document error acting strangely Python selenium 随机出现错误:元素未附加到页面文档 - Python selenium error randomly occours: element is not attached to the page document 使用Selenium和python时出现“元素未附加到页面文档”错误 - “element is not attached to the page document” error while using selenium and python Python Selenium-元素未附加到页面文档 - Python Selenium - element is not attached to the page document 带有Selenium的Python“元素未附加到页面文档” - Python with Selenium “element is not attached to the page document” Selenium 错误:元素未附加到页面文档 - Selenium error : element is not attached to the page document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM