Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Set Firefox Preferences for Selenium--Download Location

I use Selenium Marrionette and GeckoDriver to pull web data. I use the following to set my Firefox profile preferences:

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 1)
fp.set_preference("browser.helperApps.alwaysAsk.force", False)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", "H:\Downloads")
fp.set_preference("browser.download.downloadDir","H:\Downloads")
fp.set_preference("browser.download.defaultFolder","H:\Downloads")

binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True

driver = webdriver.Firefox(capabilities=firefox_capabilities, firefox_binary=binary, firefox_profile = fp)

From what I understand after reading Unable to set firefox profile preferences and FirefoxProfile passed to FirefoxDriver, it seems that nothing is being done when using firefox_profile now. So I need to implement the new updates to firefox_capabilities, but I'm not sure how to exactly do that. Any ideas?

like image 279
d84_n1nj4 Avatar asked Jan 13 '17 22:01

d84_n1nj4


People also ask

How do I change preferences in Firefox?

In the Menu bar at the top of the screen, click Firefox and select Preferences. Click the menu button. and select Settings.

How do I use Firefox options in Selenium?

FirefoxOptions options = new FirefoxOptions(); driver = new RemoteWebDriver(new URL("http://10.x.x.x:4444/wd/hub"), options); When you start your Selenium Nodes, it displays a log information on using new FirefoxOptions preferred to 'DesiredCapabilities. firefox() along with all other browser options.

Which property should be set to run Firefox browser in Selenium?

Generally to run tests on our local machine, we will just specify as WebDriver driver = new FirefoxDriver(); to run on Firefox browser. System. setProperty("webdriver.

How do I use Selenium python in Firefox?

To make Firefox work with Python selenium, you need to install the geckodriver. The geckodriver driver will start the real firefox browser and supports Javascript. Take a look at the selenium firefox code. First import the webdriver, then make it start firefox.


2 Answers

Ok, I believe I finally figured this mess out. Instead of using the code above, I used the following code which I point to my Firefox profile folder(if you need to update your default profile settings do that in Firefox before running this code):

from selenium.webdriver.firefox.options import Options
binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
fp = (r'C:\Users\username\AppData\Roaming\Mozilla\Firefox\Profiles\oqmqnsih.default')
opts = Options()
opts.profile = fp
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(capabilities=firefox_capabilities,firefox_binary=binary, firefox_options = opts)

I ran this code along with my web-scraping code and once I clicked the "Export CSV" link, it automatically downloaded as opposed to the Download Manager window popping up. Feel free to add any feedback.

like image 164
d84_n1nj4 Avatar answered Oct 14 '22 15:10

d84_n1nj4


The initial code is partialy correct. You must set browser.download.folderList value as 2 :

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2) # 0 means to download to the desktop, 1 means to download to the default "Downloads" directory, 2 means to use the directory 
fp.set_preference("browser.helperApps.alwaysAsk.force", False)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", "H:\Downloads") 

binary = FirefoxBinary(r'C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')

firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True

driver = webdriver.Firefox(capabilities=firefox_capabilities,firefox_binary=binary, firefox_profile = fp)
like image 36
Rony Rozas Avatar answered Oct 14 '22 15:10

Rony Rozas