Python Selenium: Firefox set_preference to overwrite files on download?
<p>I am using these Firefox preference setting for <code>selenium</code> in Python 2.7:</p>

<pre><code>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")
</code></pre>

<p>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.</p>

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

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

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

<p>This is something that is <em>out of the Selenium's scope</em> and is handled by the operating system.</p>

<p>Judging by the context of this and your <a href="http://stackoverflow.com/q/39519518/771848">previous question</a>, 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:</p>

<pre><code>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)
</code></pre>

python selenium firefox