Python 为 Selenium 设置 Firefox 首选项--下载位置 [英] Python Set Firefox Preferences for Selenium--Download Location

查看:68
本文介绍了Python 为 Selenium 设置 Firefox 首选项--下载位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Selenium Marrionette 和 GeckoDriver 来提取网络数据.我使用以下内容来设置我的 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 FirefoxFirefox.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 FirefoxFirefox.exe')
fp = (r'C:UsersusernameAppDataRoamingMozillaFirefoxProfilesoqmqnsih.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆