After running my below script It crawls the first link successfully and fetch the title and description but when it comes to do the same for the next link I encounter stale element reference: in this line data = [urljoin(link,item.get_attribute("href"))---. How can I complete the operation without this error?
This is the script:
from urllib.parse import urljoin
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = "http://urbantoronto.ca/database/"
driver = webdriver.Chrome()
driver.get(link)
wait = WebDriverWait(driver, 10)
for items in wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#project_list table tr[id^='project']"))):
data = [urljoin(link,item.get_attribute("href")) for item in items.find_elements_by_css_selector("a[href^='//urbantoronto']")]
#I get stale "element reference" error exactly here pointing the above line
for nlink in data:
driver.get(nlink)
sitem = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "h1.title")))
title = sitem.text
try:
desc = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".project-description p"))).text
except Exception: desc = ""
print("Title: {}\nDescription: {}\n".format(title,desc))
driver.quit()
sitemafterdriver.get(nlink). You should do this before navigating to next page. And also putwait.until(EC.staleness_of(sitem))just afterdriver.get(nlink)