Search code examples
pythonseleniumfirefoxremotewebdriver

How to download file using remote Firefox webdriver?


I've tried to adapt several existing solutions (1, 2) to the remote Firefox webdriver running in a selenium/standalone-firefox Docker container:

options = Options()
options.set_preference('browser.download.dir', '/src/app/output')
options.set_preference('browser.download.folderList', 2)
options.set_preference('browser.download.manager.showWhenStarting', False)
options.set_preference('browser.helperApps.alwaysAsk.force', False)
options.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
options.set_preference('pdfjs.disabled', True)
options.set_preference('pdfjs.enabledCache.state', False)
options.set_preference('plugin.disable_full_page_plugin_for_types', False)

cls.driver = webdriver.Remote(
    command_executor='http://selenium:4444/wd/hub',
    desired_capabilities={'browserName': 'firefox', 'acceptInsecureCerts': True},
    options=options
)

Navigating and clicking the relevant download button works fine, but the file never appears in the download directory. I've verified everything I can think of:

  • The user in the Selenium container can create files in /src/app/output and those files are visible in the host OS.
  • I can download the file successfully using my desktop browser.
  • The response content type is application/pdf.

What am I missing?


Solution

  • It turned out other changes done while researching this were resulting in the server returning a text/plain document rather than a PDF file. For reference, this is the simplest set of options I could get to work:

    options.set_preference('browser.download.dir', DOWNLOAD_DIRECTORY)
    options.set_preference('browser.download.folderList', 2)
    options.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
    options.set_preference('pdfjs.disabled', True)