Python 为 Selenium 设置 Firefox 首选项--下载位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41644381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Set Firefox Preferences for Selenium--Download Location
提问by d84_n1nj4
I use Selenium Marrionette and GeckoDriver to pull web data. I use the following to set my Firefox profile preferences:
我使用 Selenium Marrionette 和 GeckoDriver 来提取网络数据。我使用以下内容来设置我的 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)
From what I understand after reading Unable to set firefox profile preferencesand 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?
根据我在阅读Unable to set firefox profile首选项和FirefoxProfile 传递给 FirefoxDriver 后的理解,现在使用时似乎什么也没做firefox_profile
。所以我需要对 实施新的更新firefox_capabilities
,但我不确定如何做到这一点。有任何想法吗?
采纳答案by d84_n1nj4
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):
好吧,我相信我终于弄清楚了这个烂摊子。我没有使用上面的代码,而是使用了以下代码,该代码指向我的 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)
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.
我将这段代码与我的网络抓取代码一起运行,一旦我单击“导出 CSV”链接,它就会自动下载,而不是弹出下载管理器窗口。随意添加任何反馈。
回答by Rony Rozas
The initial code is partialy correct. You must set browser.download.folderList value as 2 :
初始代码是部分正确的。您必须将 browser.download.folderList 值设置为 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)
回答by basilio
the solution for my python script (on raspi 3):
我的python脚本的解决方案(在raspi 3上):
binary = FirefoxBinary('/usr/bin/firefox')
driver = webdriver.Firefox(capabilities={'browserName': 'firefox' }, firefox_binary=binary)