WebDriver并自动下载csv文件

WebDriver并自动下载csv文件

本文介绍了Firefox + Selenium WebDriver并自动下载csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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

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 expectUse HttpFox Firefox plugin (or similar) to find the real content type of the file and use it in your code

内容类型为

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

这篇关于Firefox + Selenium WebDriver并自动下载csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:38