Get this error when try to iterate using selenium. "stale element reference: element is not attached to the page document"

Issue

I write a python script. first, it visits this website. then click on the arrow on the right side and go to the new web page to collect some data. finally back to the previous page and do the same thing with next item.

Web page : https://register.fca.org.uk/s/search?q=capital&type=Companies

This is the code.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
import time

url = 'https://register.fca.org.uk/s/search?q=capital&type=Companies'
service = Service('link to come driver')
service.start()
driver = webdriver.Remote(service.service_url)
driver.get(url)
time.sleep(12)


divs = driver.find_elements_by_xpath('//div[@class="result-card_main"]')
for d in divs:

   RN = ''
   companyName = ''
   companyName =   d.find_element_by_tag_name('h2').text
   RNData = d.find_element_by_xpath('.//div[@class="result-card_figure-offset"]').text
   RN = RNData.split(':')[1].strip()

   d.click()
   time.sleep(12)

   phoneNumber = ''
   phoneNumberData =  driver.find_elements_by_xpath('//*[@id="who-is-this-details-content"]/div[1]/div[2]/div[2]/div/div/div[2]')
   phoneNumber = phoneNumberData[0].text.split('\n')[1]

   print(RN)
   print(companyName)
   print(phoneNumber)

   driver.execute_script("history.back();")

it givesme this Error:

  selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

How can I solve this problem?

Solution

Here’s a quick and dirty way to avoid that error, change your code like this:

url = 'https://register.fca.org.uk/s/search?q=capital&type=Companies'

driver.get(url)
time.sleep(12)


divs = driver.find_elements_by_xpath('//div[@class="result-card_main"]')
for i in range(len(divs)):
   time.sleep(4)
   d = driver.find_elements_by_xpath('//div[@class="result-card_main"]')
   RN = ''
   companyName = ''
   companyName = d[i].find_element_by_tag_name('h2').text
   RNData = d[i].find_element_by_xpath('.//div[@class="result-card_figure-offset"]').text
   RN = RNData.split(':')[1].strip()

   d[i].click()
   time.sleep(12)

   phoneNumber = ''
   phoneNumberData =  driver.find_elements_by_xpath('//*[@id="who-is-this-details-content"]/div[1]/div[2]/div[2]/div/div/div[2]')
   phoneNumber = phoneNumberData[0].text.split('\n')[1]

   print(RN)
   print(companyName)
   print(phoneNumber)

   driver.execute_script("window.history.go(-1)")

Answered By – C. Peck

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published