Selenium allows creating a custom profile for firefox and launching the browser with the same. Below is a sample code on how to change the download folder of the browser launched
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", '/Users/tarunlalwani/Downloads/')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
driver = webdriver.Firefox(firefox_profile=profile)
This is good when you know the download folder while starting the browser or you are NOT interested in changing the download folder after the browser has starting.
Automating configuration UI
So my first attempt was to solve the problem using about:config
UI interface
profile = webdriver.FirefoxProfile()
profile.set_preference("general.warnOnAboutConfig", False)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("about:config")
def set_bool_preferce(name, value):
value = 'true' if value else 'false';
driver.execute_script("""
document.getElementById("textbox").value = arguments[0];
FilterPrefs();
view.selection.currentIndex = 0;
if (view.rowCount == 1) {
current_value = view.getCellText(0, {id:"valeuCol"});
if (current_value != arguments[1]) {
ModifySelected();
}
}
""", name, value)
def set_string_preferce(name, value):
modified = driver.execute_script("""
document.getElementById("textbox").value = arguments[0];
FilterPrefs();
view.selection.currentIndex = 0;
if (view.rowCount == 1) {
current_value = view.getCellText(0, {id:"valeuCol"});
if (current_value != arguments[1]) {
ModifySelected();
return true;
}
}
return false;
""", name, value)
if modified is None or modified is True:
alert = driver.switch_to.alert
alert.send_keys(value)
alert.accept()
set_string_preferce("browser.download.dir", '/Users/tarunlalwani/Downloads2/')
driver.quit()
This is bit complex than I would have wanted it to be. But thanks to @JamesUp, who provided a far easier JavaScript for the same
Smaller and better JavaScript solution
profile = webdriver.FirefoxProfile()
profile.set_preference("general.warnOnAboutConfig", False)
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("about:config")
def set_bool_preferce(name, value):
value = 'true' if value else 'false';
set_string_preferce(name, value)
def set_string_preferce(name, value):
driver.execute_script("""
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
prefs.setBoolPref(arguments[0], arguments[1]);
""", name, value)
References
Change browser preferences in runtime?
Python Selenium Webdriver - Changing proxy settings on the fly