【发布时间】:2016-02-17 06:22:57
【问题描述】:
我在 Python 版本中使用了 Selenium。 但我被困在最后一步。我可以打开浏览器,登录并打开下载链接,但文件没有自动下载。
我已阅读文档,从技术上讲,我只需要使用正确的值设置 fp.set_preference("browser.helperApps.neverAsk.saveToDisk","..."),但仍然无法正常工作。
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import os
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/EDI-X12, application/EDIFACT, application/javascript, application/octet-stream, application/ogg, application/pdf, application/xhtml+xml, application/x-shockwave-flash, application/json, application/xml, application/zip, audio/mpeg, audio/x-ms-wma, audio/vnd.rn-realaudio, audio/x-wav, image/gif, image/jpeg, image/png, image/tiff, image/vnd.microsoft.icon, image/vnd.djvu, image/svg+xml, multipart/mixed, multipart/alternative, multipart/related, text/css, text/csv, text/html, text/javascript(obsolete), text/plain, text/xml, video/mpeg, video/mp4, video/quicktime, video/x-ms-wmv, video/x-msvideo, video/x-flv, video/webm, application/vnd.oasis.opendocument.text, application/vnd.oasis.opendocument.spreadsheet, application/vnd.oasis.opendocument.presentation, application/vnd.oasis.opendocument.graphics, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-powerpoint, application/vnd.openxmlformats-officedocument.presentationml.presentation, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.mozilla.xul+xml")
browser = webdriver.Firefox(firefox_profile=fp)
browser.get('https://www4.webcas.net/mail02/menu')
browser.find_element_by_xpath("//input[@type='text']").send_keys('login')
browser.find_element_by_xpath("//input[@type='password']").send_keys('pass' + Keys.RETURN)
time.sleep(2) # delays for 2 seconds
browser.get('https://www4.webcas.net/mail02/fm/onetime-ticket?to=enquete')
time.sleep(2) # delays for 2 seconds
browser.get('https://www4.webcas.net/form02/operator/formulator/download?enquete_id=4770')
time.sleep(2) # delays for 2 seconds
最后,我总是有 firefox 弹出窗口“你要打开还是保存”。
是不是我做错了什么?
【问题讨论】:
-
你有什么错误吗?
标签: windows python-2.7 selenium selenium-webdriver