简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM