Firefox + Selenium WebDriver 并自动下载 csv 文件

2022-01-16 00:00:00 python csv selenium webdriver firefox

问题描述

我对 Selenium WebDriver 和 Firefox 有疑问.我想下载 csv 文件而不在对话框窗口中确认,我有这样的代码:

I have problem with Selenium WebDriver and Firefox. I want to download csv file without confirmation in dialog window and I have code like this:

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.dir", download_dir)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")

但它似乎不起作用.我尝试了许多与 browser.helperApps.neverAsk.saveToDisk

but it seems not working. I tried many combination with browser.helperApps.neverAsk.saveToDisk

fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv,application/csv,text/plan,text/comma-separated-values")

fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/csv")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/plain")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/comma-separated-values")

但没有区别,Firefox 不会自动下载.我该如何解决?

but there's no difference and Firefox won't download automaticly. How can I fix it?


解决方案

有时内容类型不符合您的预期使用 HttpFox Firefox 插件(或类似插件)查找文件的真实内容类型并在您的代码中使用它

Sometime the content type is not as you'd expect Use HttpFox Firefox plugin (or similar) to find the real content type of the file and use it in your code

顺便说一句,对我来说内容类型是

BTW, For me the content type was

fp.set_preference("browser.helperApps.neverAsk.openFile", "application/octet-stream");
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");

相关文章