firefox中的文件下载对话框

时间:2011-12-28 12:43:05

标签: python selenium

iam使用firefox进行selenium + python编程,自动启动下载并保存file.i已完成所有操作但无法下载csv文件。    我的python版本是2.6.6,我的selenium版本是最新版本。    我尝试使用以下链接(即)

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

我使用了这个,但我得到了文件,我也没有发现任何错误。 任何一个PLZ帮助我..

我的文件是![我到了这里,我的下一步是使用selenium + python程序下载] [1]

如果有人有解决方案,请帮助我。

1 个答案:

答案 0 :(得分:2)

这是一个完整的示例,适用于我使用Firefox 3.6.24和8.0.1。

#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
from selenium import webdriver

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.dir',"/tmp/webdriver-downloads")
profile.set_preference('browser.download.folderList',2)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',"text/csv")
driver = webdriver.Firefox(profile)
base_url = "http://localhost/"
driver.get(base_url + "/text.csv")

您确定您的网络服务器正在将text / csv作为Mime类型返回吗?一种验证方法是使用curl确认HTTP响应中的Content-Type标头是您所期望的:

$ curl -v http://localhost/text.csv
* About to connect() to localhost port 80 (#0)
*   Trying 127.0.0.1... connected
> GET /text.csv HTTP/1.1
> User-Agent: curl/7.23.1 (x86_64-apple-darwin10.8.0) libcurl/7.23.1 OpenSSL/1.0.0e zlib/1.2.5 libidn/1.22
> Host: localhost
> Accept: */*
> 
< HTTP/1.1 200 OK
< Date: Wed, 28 Dec 2011 17:10:46 GMT
< Server: Apache/2.2.20 (Unix) mod_ssl/2.2.20 OpenSSL/0.9.8r DAV/2
< Last-Modified: Wed, 28 Dec 2011 17:05:47 GMT
< ETag: "291f98-0-4b52a02cbb0c0"
< Accept-Ranges: bytes
< Content-Length: 0
< Cache-Control: max-age=300
< Expires: Wed, 28 Dec 2011 17:15:46 GMT
< Content-Type: text/csv
< 
* Connection #0 to host localhost left intact
* Closing connection #0