Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Selenium: Firefox set_preference to overwrite files on download?

I am using these Firefox preference setting for selenium in Python 2.7:

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")

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).

like image 777
P A N Avatar asked Oct 18 '22 01:10

P A N


1 Answers

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):
    os.remove(filepath)
like image 198
alecxe Avatar answered Oct 21 '22 04:10

alecxe