Forums

Selenium doesn't download file

Hi! I'm currently writing a webapp with Flask that automatically update a db, parsing various official statistics italian websites. One of the functions should download an excel file on the Bank of Italy website through selenium, with the following code

download_dir = '/home/fpacicco/app/material'

listurl = ['https://infostat.bancaditalia.it/inquiry/home?spyglass/taxo:CUBESET=/PUBBL_00/PUBBL_00_07/PUBBL_00_07_02/TFR20232&ITEMSELEZ=TFR20232_52000700:true&OPEN=false/&ep:LC=IT&COMM=BANKITALIA&ENV=LIVE&CTX=DIFF&IDX=2&/view:CUBEIDS=TFR20232_52000700/']

# Open a browser
chrome_options = webdriver.ChromeOptions()
preferences = {"download.default_directory": download_dir, "directory_upgrade": True, "download.prompt_for_download": False}
chrome_options.add_experimental_option("prefs", preferences)
chrome_options.add_experimental_option('w3c', False)
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--headless")

try:
    driver.close()
except:
    pass

driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()

driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
command_result = driver.execute("send_command", params)

for tempurl in listurl:
    driver.get(tempurl)

    try:
        element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "pvtRendererArea")))
    finally:
        time.sleep(7)
        # run some javascript in order to optimize file before download
        driver.execute_script("javascript:moveDimension('LOC_CTP', 'OUT', 'ROWS');")

    time.sleep(7)
    driver.find_element_by_id('exportProspectus').click()
    time.sleep(2)
    try:
        driver.find_element_by_id('excel').click()
    except:
        driver.find_element_by_name('radioFormatoEsporta').click()
    time.sleep(2)
    element = driver.find_element_by_id('applicaExport').click()
    time.sleep(15)
    driver.get_screenshot_as_file('/home/fpacicco/app/material/control_status.png')


driver.close()

This is looped on other 5 links, here omitted. However, despite the several attempts to set the download path, I am not able to see any download, even if the get_screenshot_as_file shows that the "export request" on the website ended successfully. I'm running out of ideas, can someone please help me on how to solve this issue? Is there anything wrong with my code? Thanks!

Are you perhaps not waiting long enough for the download to complete?

Hi glenn. I've just tried to increase the last time.sleep to 60 seconds, but it still doesn't work. The file is less than 700kb, so maybe it is not this the issue, or am I wrong?

Then I'm afraid I'm out of ideas.