【问题标题】:Python Selenium: Firefox set_preference to overwrite files on download?Python Selenium:Firefox set_preference 在下载时覆盖文件?
【发布时间】:2017-01-24 00:14:52
【问题描述】:

我在 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).extfilename (2).ext 等的副本。

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

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

【问题讨论】:

    标签: python selenium firefox


    【解决方案1】:

    这是 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)
    

    【讨论】:

    • 开启无头设置后,为了使下载正常工作,需要进行设置,Dictionary Params = new Dictionary();参数[“行为”] =“允许”;参数 ["downloadPath"] = 下载目录; driver.ExecuteChromeCommand("Page.setDownloadBehavior", Params);我发现的主要问题是上述确切行为正在发生。除了,我想要默认的 OS\Windows\default 浏览器行为。我怎样才能找回它?
    猜你喜欢
    • 2016-02-11
    • 2016-03-16
    • 2012-01-22
    • 2019-01-02
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    相关资源
    最近更新 更多