10

我使用 Selenium Marrionette 和 GeckoDriver 来提取 Web 数据。我使用以下设置我的 Firefox 配置文件首选项:

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)

根据我在阅读Unable to set firefox profile preferencesFirefoxProfile 传递给 FirefoxDriver后的理解,现在使用时似乎没有做任何事情firefox_profile。所以我需要实施新的更新firefox_capabilities,但我不确定如何准确地做到这一点。有任何想法吗?

4

3 回答 3

9

好吧,我相信我终于弄清楚了这个烂摊子。我没有使用上面的代码,而是使用了指向我的 Firefox 配置文件文件夹的以下代码(如果您需要在运行此代码之前在 Firefox 中更新默认配置文件设置):

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)

我将此代码与我的网络抓取代码一起运行,一旦我单击“导出 CSV”链接,它就会自动下载,而不是弹出下载管理器窗口。随意添加任何反馈。

于 2017-01-16T19:05:09.950 回答
3

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)
于 2018-03-01T10:56:54.370 回答
1

我的 python 脚本的解决方案(在 raspi 3 上):

binary = FirefoxBinary('/usr/bin/firefox')
driver = webdriver.Firefox(capabilities={'browserName': 'firefox' }, firefox_binary=binary)
于 2017-11-27T15:19:47.720 回答