2011-12-28 76 views
3

iam通过使用firefox工作在selenium + python编程中,自动启动下载并保存文件。我已经做了所有事情,但无法下载csv文件。 我的Python版本是2.6.6,我的硒版本是最新版本。 我试图通过以下链接也(即)firefox中的文件下载对话框

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) 

我用这个,但我不力获取文件和IAM不是占有任何错误也。 任何一个plz帮助我..

我的文件![我得到了到这里,我的下一个步骤是使用硒+的Python程序进行下载] [1]

如果任何一个有解决方案PLZ帮我。

回答

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") 

您确定您的web服务器正在返回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