Winterflags Winterflags - 1 year ago 187
Python Question

Python Selenium: Firefox set_preference to overwrite files on download?

I am using these Firefox preference setting for

in Python 2.7:

ff_profile = webdriver.FirefoxProfile(profile_dir)

ff_profile.set_preference("", 2)
ff_profile.set_preference("", False)
ff_profile.set_preference("", dl_dir)
ff_profile.set_preference('browser.helperApps.neverAsk.saveToDisk', "text/plain, application/, text/csv, text/comma-separated-values, application/octet-stream")

With Selenium, I want to recurringly download the same file, and overwrite it, thus keeping the same filename – without me having to confirm the download.

With the settings above, it will download without asking for location, but all downloads will creates duplicates with the filename
filename (1).ext
filename (2).ext
etc in MacOS.

I'm guessing there might not be a setting to allow overwriting from within Firefox, to prevent accidents(?).

(In that case, I suppose the solution would be to handle the overwriting on the disk with other Python modules; another topic).

Answer Source

This is something that is out of the Selenium's scope and is handled by the operating system.

Judging by the context of this and your previous question, you know (or can determine from the link text) the filename beforehand. If this is really the case, before hitting the "download" link, make sure you remove the existing file:

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):
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download