0

I am trying to download the file automatically from system file download dialog by setting preference in firefox profile in my python selenium code , but my code is not working.

Browser : Firefox 72.0 Selenium Version : 3.14 OS : linux Ubuntu Filetype to download: *.enc (encrypted file type) Path of firefox in linux : /usr/bin/firefox

Code :

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", "/home/user/Downloads/tests")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-uuencoded,application/octet-stream")
self.driver = webdriver.Firefox(firefox_profile=profile)

Windows download dialog

Sum
  • 363
  • 2
  • 4
  • 16
  • Can someone suggest if we have to set any different preference for *.enc file . Also in firefox profile setting even if I have set ("browser.download.manager.showWhenStarting", False) still the dialog box is visible when the test clicks on download link. – Sum Feb 14 '20 at 05:17

4 Answers4

1

Hi @Sum i have resolving, my problem was a different Content-Type.

Use this example to resolve your problem , and to understand your Content-Type: https://stackoverflow.com/a/36356422/12911814

In my case the Content-Type was "application/force-download" not "application/pdf"

    profile.set_preference("pdfjs.disabled", True)
    profile.set_preference("browser.download.folderList",2)
    profile.set_preference("browser.download.manager.useWindow", False)
    profile.set_preference("browser.download.dir", "<path>")
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf, application/force-download")

These settings worked for me. Hope it might help you.

0

I have the same problem with Firefox 72.0, but with pdf files. This is the code:

fp = webdriver.FirefoxProfile()
fp.set_preference("pdfjs.disabled", True)
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.dir", "/path")
fp.set_preference("browser.download.downloadDir", "/path")
fp.set_preference("browser.download.defaultFolder", "/path")
fp.set_preference("plugin.disable_full_page_plugin_for_types", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")
fp.set_preference("browser.helperApps.neverAsk.openFile", "application/x-pdf, application/acrobat, applications/vnd.pdf, text/pdf, text/x-pdf, application/vnd.cups-pdf")

driver = webdriver.Firefox(firefox_profile=fp) 

I have tried all possible preferences, but it always triggers the download alert.

Jir
  • 2,985
  • 8
  • 44
  • 66
  • 1
    Hi @SantinoMagno, and welcome! Given this doesn't look like an answer, may I suggest you turn it into a comment to the main question? This way it'd be clearer it doesn't try to address the original issue. Alternatively, if it's different enough, consider posting your own question! – Jir Feb 17 '20 at 10:39
  • Thanks jir, sorry but i haven't reputation to comment the main question... – Santino Magno Feb 17 '20 at 11:30
  • No worries - I didn't know you were required to have some rep to be able to do that! – Jir Feb 17 '20 at 11:47
0

The correct MIME type for .enc is "text/x-uuencoded" Updated as below in code and it's working :

profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/x-uuencoded")
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
Sum
  • 363
  • 2
  • 4
  • 16
0

Try this will work like charm ......

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    import time
    import pyautogui

    try :



        driver = webdriver.Firefox()

        driver.implicitly_wait(30)
        driver.maximize_window()

        driver.get("https://www.citysdk.eu/wp-content/uploads/2013/09/DELIVERABLE_WP4_TA_SRS_0.21.pdf")
        WebDriverWait(driver, 10).until(lambda d: d.execute_script('return document.readyState') == 'complete')
        # Click the OK button and close

        time.sleep(5)
        webelem = driver.find_element_by_id('download')
        webelem.click()

        time.sleep(5)
        print('press enter')
        pyautogui.press('enter')


    except Exception as err:
        print('ERROR: %sn' % str(err))

        driver.quit()
niraj rahi
  • 57
  • 1
  • 5