2

I have tried various methods but I'm unable to download excel file. I always end up with the firefox popup staring at me. any Idea how to get this working

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

    profile = webdriver.FirefoxProfile()
    profile.set_preference('browser.download.folderList', 2) # customlocation
    profile.set_preference('browser.download.dir','/home/rahul')
    profile.set_preference('browser.download.manager.showWhenStarting', False)
    profile.set_preference('browser.download.panel.shown', False)
    profile.set_preference('browser.helperApps.neverAsk.saveToDisk','application/vnd.ms-excel, application/vnd.ms-excel.addin.macroenabled.12,application/vnd.ms-excel.sheet.binary.macroenabled.12,application/vnd.ms-excel.template.macroenabled.12,application/vnd.ms-excel.sheet.macroenabled.12,application/octet-stream')
    profile.set_preference("browser.helperApps.alwaysAsk.force", False)
    profile.set_preference("browser.download.manager.alertOnEXEOpen", False)
    profile.set_preference("browser.download.manager.focusWhenStarting", False)
    profile.set_preference("browser.download.manager.useWindow", False)
    profile.set_preference("browser.download.manager.showAlertOnComplete", False)
    profile.set_preference("browser.download.manager.closeWhenDone", False)
    driver = webdriver.Firefox(profile)
    driver.get('any_url')
    driver.wait = WebDriverWait(driver, 5)
    time.sleep(5)
    export = driver.wait.until(EC.element_to_be_clickable((By.ID, "ExportSpn"))).click()
    time.sleep(5)
    driver.quit()
  • 1
    See the most upvoted answer. Possible duplicate of [Python: Unable to download with selenium in webpage](https://stackoverflow.com/questions/44072022/python-unable-to-download-with-selenium-in-webpage/44196146#44196146) – undetected Selenium Dec 29 '17 at 13:48
  • 1
    I got it done thanks for the suggestion. – Rahul Mishra Jan 02 '18 at 06:12

1 Answers1

0

I was mentioning the MIME type wrong. The MIME type require the download file was 'application/ms-excel'. If someone still has problems getting MIME type wrong I would suggest https://stackoverflow.com/users/771848/alecxe approach to get MIME type right.

• manually download the file with Firefox checking the "automatically save this file type" checkbox • open Help -> Troubleshooting Information in firefox browser • locate the "Profile Folder" button, click it • inside the profile folder locate the mimeTypes.rdf file • open the file in a text editor and look for the mimetypes mentioned there - the XML node attribute values that start with urn:mimetype • use the mimetypes you found in the browser.helperApps.neverAsk.saveToDisk comma-separated value