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

萝らか妹 提交于 2020-08-20 14:20:23

问题


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)

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)

The error is in line 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.


回答1:


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.

Use try..except block to handle.

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



回答2:


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:

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)


来源:https://stackoverflow.com/questions/63191382/python-selenium-i-am-getting-element-is-not-attached-to-the-page-document-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!