【问题标题】:python : disable download popup when using firefox with seleniumpython:使用带有 selenium 的 firefox 时禁用下载弹出窗口
【发布时间】:2018-01-20 14:06:53
【问题描述】:

我有使用 selenium 和 firefox 自动执行下载操作的脚本。 问题是每当我运行脚本时,我总是会从 firefox 弹出,不断询问我想做什么样的操作,即使我在 firefox 首选项中设置了下载路径。我检查了文件和文件夹以为所有用户创建主 mimeTypes.rdf,但我找不到我的。(我使用的是 ubuntu)。我找到了 ~/.mozilla/firefox,但没有我的配置文件名称目录的文件,也没有任何文件具有类似 .rdf 的扩展名

这是让我抓狂的罪犯照片

firefox download popup

以下是我为禁用弹出窗口所做的操作。

profile = FirefoxProfile()
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile", 'application/zip')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", 'application/zip')
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.dir", "/home/i-06/Downloads")
driver = webdriver.Firefox(firefox_profile=profile)

【问题讨论】:

标签: javascript python selenium firefox


【解决方案1】:

我花了很多时间试图抑制使用带有 selenium 的 Firefox 驱动程序(python 3.x)下载文件时出现的“保存或打开”弹出窗口。涉及profile.set_preference 的各种值的许多建议都不适合我。也许我错过了什么。

不过,我终于通过推荐的另一种方法让它工作了:使用现有的 firefox 配置文件。

您可以将默认(或自定义)配置文件调整为您想要的文件保存行为。在 firefox 地址栏中输入以下内容并在此处进行更改:

about:preferences#applications

那么,将文件下载到当前工作目录所需的唯一设置是:

from selenium import webdriver
fp = webdriver.FirefoxProfile(<your firefox profile directory>)
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.dir", os.getcwd())
driver = webdriver.Firefox(firefox_profile=fp)

如果你有一个典型的 ubuntu 设置,你可以通过查看 ~/.mozilla/firefox/profile.ini 找到你的默认 firefox 配置文件目录

在该 .ini 文件中,查找 [Profile0] 下的路径

【讨论】:

  • 对不起,我没有得到“()”部分。引号或 [profile0] 中的目录地址应该是什么?就我而言,我看不到任何 [Profile0]
  • @sangharsh : 你能在这里打印你的profiles.ini文件吗?
  • 我一定会的。给我一些时间
【解决方案2】:

我怀疑您需要同时定义两者。从您的代码中删除以下行

profile.set_preference("browser.helperApps.neverAsk.openFile", 'application/zip')

有时,zip 文件的 MIME 类型可能会因服务器而异。它可以是以下任何一种

  • 应用程序/八位字节流
  • 多部分/x-zip
  • 应用程序/zip
  • 应用程序/zip 压缩
  • application/x-zip-compressed

因此,在“网络”选项卡中检查您获得的内容类型是什么,并将其添加到您的个人资料中以确保不会出现对话框

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-25
    • 2021-11-21
    • 2018-06-21
    • 2016-07-21
    • 2019-05-04
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    相关资源
    最近更新 更多