gpt4 book ai didi

javascript - python : disable download popup when using firefox with selenium

转载 作者:行者123 更新时间:2023-11-28 22:25:15 27 4
gpt4 key购买 nike

我有使用 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)

最佳答案

我花了很多时间试图抑制在使用 selenium (python 3.x) 的 firefox 驱动程序下载文件时出现的“保存或打开”弹出窗口。涉及 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] 下查找 Path

关于javascript - python : disable download popup when using firefox with selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45645648/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com