0
我試圖從網頁下載所有的xml文件。該過程要求依次定位xml文件下載鏈接,並且一旦點擊了這樣的下載鏈接,就會導致需要提交下載的表單。我面臨的問題在於這些循環的迭代,一旦從網頁下載第一個文件,我收到一個錯誤:Python硒:迭代錯誤
「selenium.common.exceptions.StaleElementReferenceException:消息:stale的元素引用:該元素不再連接到DOM或頁面已被刷新「
」97081 data-extension xml「是迭代中的第二個可下載文件。我在此附上代碼,任何建議,以糾正這將非常感激。
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()