1

I am writing a script using Selenium to export an XML backup from the export page of a MediaWiki-based website.

I wish to download the XML file to a directory, bypassing the popup window that usually asks me what to do with the file.

When I look at the Network tab when downloading the .xml, file I see (under Response Headers) that content-type is application/xml; charset=utf-8.

Screenshot from Network tab on Firefox

After reading some answers here, as well as this reddit post, I am using:

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.preferences.instantApply",True)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/plain, application/octet-stream, text/xml, application/xml")
fp.set_preference("browser.helperApps.alwaysAsk.force",False)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.folderList",2)
    
driver = webdriver.Firefox(firefox_profile=fp, executable_path=r'C:\Python37\geckodriver.exe')
(...)
download_button.click()

However, the window still pops up and the file is not downloaded anywhere.

I made sure to update Selenium and the geckodriver.

What am I doing wrong?

Thank you!

Paek Se
  • 111
  • 4

2 Answers2

1

After much trial-and-error for me this worked (it's Java but should be transferable to Python):

profile.setPreference("browser.download.viewableInternally.previousHandler.alwaysAskBeforeHandling.xml", false);
profile.setPreference("browser.download.viewableInternally.previousHandler.preferredAction.xml", 0);
profile.setPreference("browser.download.viewableInternally.typeWasRegistered.xml", true);
ciis0
  • 311
  • 1
  • 9
0

I think you should pass a tuple as an argument, like this:

fp.set_preference('browser.helperApps.neverAsk.saveToDisk', (text/plain, application/octet-stream, text/xml, application/xml))

It works for me. PS: Ignore my poor english, I'm brazilian.