gpt4 book ai didi

python Selenium : Firefox set_preference to overwrite files on download?

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:12 24 4
gpt4 key购买 nike

我在 Python 2.7 中为 selenium 使用这些 Firefox 首选项设置:

ff_profile = webdriver.FirefoxProfile(profile_dir)

ff_profile.set_preference("browser.download.folderList", 2)
ff_profile.set_preference("browser.download.manager.showWhenStarting", False)
ff_profile.set_preference("browser.download.dir", dl_dir)
ff_profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream")

使用 Selenium,我想反复下载相同的文件并覆盖它,从而保持相同的文件名——无需我确认下载。

使用上面的设置,它会在不询问位置的情况下下载,但是所有下载都会创建文件名 filename (1).extfilename (2).ext 等。

我猜可能没有允许从 Firefox 中覆盖的设置,以防止意外(?)。

(在那种情况下,我想解决方案是用其他 Python 模块处理磁盘上的覆盖;另一个主题)。

最佳答案

这是 Selenium 的范围,由操作系统处理的事情。

根据这个和你的上下文判断previous question ,您事先知道(或可以从链接文本确定)文件名。如果确实如此,请确保在点击“下载”链接之前删除现有文件:

import os

filename = "All-tradable-ETFs-ETCs-and-ETNs.xlsx" # or extract it dynamically from the link
filepath = os.path.join(dl_dir, filename)
if os.path.exists(filepath):
os.remove(filepath)

关于 python Selenium : Firefox set_preference to overwrite files on download?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39520095/

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