J'essaie de télécharger tous les fichiers XML à partir d'une page Web. Le processus nécessite de localiser le lien de téléchargement de fichier xml l'un après l'autre, et une fois qu'un tel lien de téléchargement est cliqué, il conduit à un formulaire qui doit être soumis pour le téléchargement pour commencer. Le problème que je suis confronté réside dans l'itération de ces boucles, une fois le premier fichier téléchargé à partir de la page Web, je reçois une erreur:Sélénium Python: erreur d'itération
"selenium.common.exceptions.StaleElementReferenceException: Message: La référence de l'élément de stale: soit l'élément n'est plus attaché au DOM ou la page a été rafraîchie "
Le" xml d'extension de données 97081 "est le deuxième fichier téléchargeable de l'itération. J'ai joint le code, toutes les suggestions pour rectifier cela seront très appréciées.
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()