我在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,我想重复下载相同的文件,并覆盖它,从而保持相同的文件名 - 无需我确认下载。
使用上述设置,它会在不询问位置的情况下下载,但所有下载都会在MacOS中创建文件名为filename (1).ext
,filename (2).ext
等的重复项。
我猜测可能没有设置允许在Firefox中覆盖,以防止意外(?)。
(在这种情况下,我认为解决方案是用其他Python模块处理磁盘覆盖;另一个主题)。
答案 0 :(得分:3)
这是超出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)