【问题标题】:Selenium: automatic downloadSelenium:自动下载
【发布时间】: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


【解决方案1】:

我找到了解决办法。

方法是正确的,但参数不正确。 我不得不检查文件 C:\Users\User\AppData\Roaming\Mozilla\Firefox\Profiles\tofzlgfm.default\mimeTypes.rdf

我在哪里找到了以下几行

  <RDF:Description RDF:about="urn:mimetype:text/*"
                   NC:value="text/*"
                   NC:editable="true"
                   NC:fileExtensions="zip"
                   NC:description="WinZip File">
    <NC:handlerProp RDF:resource="urn:mimetype:handler:text/*"/>
  </RDF:Description>

所以,我只是修改了我的行,将 text/* 添加到其他值

我希望这对某人有用。

【讨论】:

    【解决方案2】:

    我在使用 pdf 文件时遇到了同样的问题,为了解决这个问题,我还在配置文件中添加了以下内容

    fp.set_preference("browser.helperApps.neverAsk.openFile", "application/pdf")
    fp.set_preference("pdfjs.disabled", True)
    

    因此,在您的情况下,您应该更改文件类型以匹配您想要的文件类型,并且可以忽略第二行。希望这会有所帮助,干杯。

    加分项:要提高性能,请禁用图片加载

    fp.set_preference("permissions.default.image", 2)
    

    【讨论】:

      猜你喜欢
      • 2015-01-09
      • 2019-01-23
      • 2021-10-08
      • 2019-01-27
      • 1970-01-01
      • 2015-08-07
      • 2021-11-16
      • 2013-09-16
      • 1970-01-01
      相关资源
      最近更新 更多