Zeinab Abbasimazar
Zeinab Abbasimazar

Reputation: 10479

set_preference does not work on FireFox profile

I have a python script which is trying to download an excel file automatically. I've tried to address firefox profile directly and it works fine:

path = '\\path\\to\\firefox\\default\\profile\\'
fp = webdriver.FirefoxProfile(path)
driver = webdriver.Firefox(firefox_profile=fp)
driver.maximize_window()
driver.get('url') 
driver.find_element_by_id('downloadButton').click()
time.sleep(5)
driver.close()

But when I try to set a profile programmatically, it does not work; there is still download pop-up:

fp = webdriver.FirefoxProfile()
fp.set_preference('browser.download.folderList', 2)
fp.set_preference('browser.download.manager.showWhenStarting', False)
fp.set_preference('browser.download.dir', os.getcwd())
fp.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv/xls/xlsx')
driver = webdriver.Firefox(firefox_profile=fp)
driver.maximize_window()
driver.get('url') 
driver.find_element_by_id('downloadButton').click()
time.sleep(5)
driver.close()

I also changed the line fp.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv/xls/xlsx') to fp.set_preference('browser.helperApps.neverAsk.saveToDisk', ('application/vnd.ms-excel')); but there was no changes.

Any idea what did I wrong?

EDIT 1:

I have tried

fp.set_preference('browser.helperApps.neverAsk.saveToDisk', "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream")

and

fp.set_preference('browser.helperApps.neverAsk.saveToDisk', "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

There is no changes in the results.

Upvotes: 2

Views: 8469

Answers (2)

Pierre-Arthur Ferraro
Pierre-Arthur Ferraro

Reputation: 144

This work fine for me in Java, i think this gonna work in Python to.

FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setAcceptUntrustedCertificates(true);
firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir","C:\\Users\\Administrateur\\Downloads\\");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream");
driver = new FirefoxDriver(firefoxProfile);

Upvotes: 0

alecxe
alecxe

Reputation: 474201

According to about:config docs, browser.helperApps.neverAsk.saveToDisk preference value should be a comma-separated list of MIME-types.

This one should work for you:

fp.set_preference('browser.helperApps.neverAsk.saveToDisk', "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream")

Demo (using XLSX Sample web page):

import os
from selenium import webdriver
import time

url = "http://file-sample.com/xlsx/"

# configuring profile
fp = webdriver.FirefoxProfile()
fp.set_preference('browser.download.folderList', 2)
fp.set_preference('browser.download.manager.showWhenStarting', False)
fp.set_preference('browser.download.dir', os.getcwd())
fp.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

# open the web page and download the file
driver = webdriver.Firefox(firefox_profile=fp)
driver.maximize_window()
driver.get(url)
driver.find_element_by_xpath('//div[@class="post-entry"]//a').click()
time.sleep(5)
driver.close()

As the result, the file is downloaded in the current working directory.

Upvotes: 7

Related Questions