I am using Selenium on Python and trying to move the cursor and click on a specific element. This works for the first link and the structure of the HTML is the same for the next link but I get a StaleElementReferenceException for the second link when accessing it through the same webdriver. Why does this happen and how do I fix it? Below is the code I am running. Thank you so much!
def getZest(url):
zestlist = []
yearlist = []
driver.get(url)
time.sleep(5)
result = False;
attempts = 0;
while(attempts < 5):
try:
Home_Value = wait.until(EC.presence_of_element_located((By.XPATH, "//a[text()='Home value']")))
action.move_to_element(Home_Value).click().perform()
zestimate = driver.find_element_by_xpath('//*[@id="ds-home-values"]/div/div[3]/button')
action.move_to_element(zestimate).perform()
result = True
break
except exceptions.StaleElementReferenceException as e:
print(e)
attempts = attempts + 1
fivenums = ["https://www.zillow.com/homedetails/212-Haddrell-St-Mount-Pleasant-SC-29464/10922911_zpid/", "https://www.zillow.com/homedetails/20-Grove-St-Hicksville-NY-11801/31127407_zpid/"]
for num in fivenums:
getZest(num)
I was able to get informations about the first and second link with the following code, without any error:
In your code there it's not showed where some variables are instantiated so, maybe this is were the problem is located.
However when opening the first link, the website showed me the Google Captcha protection, so, I suppose you have some kind of authorization to scrape the informations with the permission of the owner.