2

The issue I'm facing lies in the iteration of these loops, once the first file is downloaded from the webpage I receive an error:

"selenium.common.exceptions.StaleElementReferenceException: Message: The element reference of stale: either the element is no longer attached to the DOM or the page has been refreshed"

The "97081 data-extension xml" is the 2nd downloadable file in the iteration. I've hereby attached the code, any suggestions to rectify this will be much appreciated.

import os
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

fp = webdriver.FirefoxProfile()

fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
fp.set_preference("browser.download.dir", "F:\Projects\Poli_Map\DatG_Py_Dat")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/xml")

driver = webdriver.Firefox(firefox_profile=fp)

driver.get('https://data.gov.in/catalog/variety-wise-daily-market-prices-data-cauliflower')
wait = WebDriverWait(driver, 10)

allelements = driver.find_elements_by_xpath("//a[text()='xml']")

for element in allelements:
    element.click()
    class FormPage(object):
        def fill_form(self, data):
            driver.execute_script("document.getElementById('edit-download-reasons-non-commercial').click()")
            driver.execute_script("document.getElementById('edit-reasons-d-rd').click()")
            driver.find_element_by_xpath('//input[@name = "name_d"]').send_keys(data['name_d'])
            driver.find_element_by_xpath('//input[@name = "mail_d"]').send_keys(data['mail_d'])
            return self

        def submit(self):
            driver.execute_script("document.getElementById('edit-submit').click()")

    data = {
        'name_d': 'xyz',
        'mail_d': '[email protected]',
    }
    time.sleep(5)
    FormPage().fill_form(data).submit()

    time.sleep(5)
    window_before = driver.window_handles[0]
    driver.switch_to_window(window_before)
    driver.back()

2 Answers 2

8

Once page refreshed after you call submit() webElements inside allelements are no more valid

Instead of

allelements = driver.find_elements_by_xpath("//a[text()='xml']")

for element in allelements:
    element.click()

try

allelements = len(driver.find_elements_by_xpath("//a[text()='xml']"))

for index in range(allelements):
    driver.find_elements_by_xpath("//a[text()='xml']")[index].click()
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you Andersson, for the timely response. This works fine.
this doesn't work for me. After the click, I use driver.execute_script("window.history.go(-1)") to go back to the previous page. I got "IndexError: list index out of range" when click the next item.
@Heinz , this seem to be a timing issue: when you get back there are no elements yet... try to implement ExplicitWait
@Andersson I implemented the wait, but still get the same error no matter how long the timeout is set. It appears that the driver can not focus on the page with the links after driver.execute_script("window.history.go(-1)").
does it work differently with driver.back()? You can submit new ticket regarding this issue as there is not enough info to provide you with a good solution
|
0
>>>Customize the for loop as:  

from selenium.common import exceptions  

and customize your code of for loop as:  

for element in allelements:
   try:  
     //your code to find element and operations with that element
   except exceptions.StaleElementReferenceException:  
        pass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.