'Download file with Python and Selenium

Good morning,

I have been trying to download a xls file from this site using python and selenium. enter image description here

enter image description here I tried applying the folloing tutorians and didn't manage to download it.

-https://titanwolf.org/Network/Articles/Article?AID=88fd6b3b-09e2-4542-8166-a498245d1a04

Every time I try it, the pop-up window appears again requesting tho select one option to download (I'd like to avoid it and set it automatically).

enter image description here

Bellow is my code:

import os
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
from selenium.common.exceptions import TimeoutException


fp = webdriver.FirefoxProfile()
fp.set_preference("driver.download.folderList",2)
fp.set_preference("driver.download.manager.showWhenStarting",False)
fp.set_preference("driver.download.panel.shown", False)
# fp.set_preference("driver.download.dir", os.getcwd())
fp.set_preference("driver.download.dir", '/home/home/alvaro/Documentos/Alvaro/Estudos_Python/get_crimes')
fp.set_preference("driver.helperApps.neverAsk.saveToDisk", "application/xls")
fp.set_preference("driver.helperApps.neverAsk.openFile","application/xls")

driver = webdriver.Firefox(firefox_profile=fp)

driver.get('https://www.ssp.sp.gov.br/transparenciassp/')

crime_class = driver.find_elements(By.CSS_SELECTOR, '*[class^="col-lg-4 col-md-4 col-sm-4 col-xs-4 nopadd centered"')
crime_class_text = [crime_class[i].text for i in  range(len(crime_class))]

for crime in crime_class:
    if (crime.text == 'NOTA EXPLICATIVA' or
        crime.text == 'TAXA DE HOMICÍDIO' or
        crime.text == 'ESTATÍSTICA' or
        crime.text == ''):
        pass
    else:
        crime.click()
        wait = WebDriverWait(driver, 10)
        wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,'table')))

        btn_export = driver.find_element(By.ID, 'cphBody_ExportarBOLink').click()
    
    crime_class = driver.find_elements(By.CSS_SELECTOR, '*[class^="col-lg-4 col-md-4 col-sm-4 col-xs-4 nopadd centered"')


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source