Python为Selenium设置Firefox首选项-下载位置 [英] Python Set Firefox Preferences for Selenium--Download Location
问题描述
我使用Selenium Marrionette和GeckoDriver提取Web数据.我使用以下设置我的Firefox配置文件首选项:
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)
根据我的理解,阅读无法设置Firefox配置文件首选项和将FirefoxProfile传递给FirefoxDriver ,现在使用firefox_profile
似乎没有任何反应.因此,我需要对firefox_capabilities
实施新的更新,但是我不确定如何准确地做到这一点.有什么想法吗?
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?
推荐答案
好吧,我相信我终于弄明白了.我没有使用上面的代码,而是使用了以下代码,该代码指向我的Firefox配置文件文件夹(如果您需要更新默认的配置文件设置,请在运行此代码之前在Firefox中执行此操作)
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)
我同时运行了此代码和网络抓取代码,一旦单击导出CSV"链接,它就会自动下载,而不是弹出下载管理器"窗口.随时添加任何反馈.
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.
这篇关于Python为Selenium设置Firefox首选项-下载位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!