Rename downloaded files selenium

Question:

I’m using selenium to automatically download files in csv format from this page:

https://catalog.data.gov/dataset?tags=crime

This is the code I’m using:

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", '/home/luis/Desktop/data/')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")

driver = webdriver.Firefox(firefox_profile=profile)
driver.get(url)
time.sleep(2)
download_button = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[2]/section[1]/div[2]/ul/li[14]/div/ul/li[1]/a')
download_button.click()

Here the download folder is set:

profile.set_preference("browser.download.dir", '/home/luis/Desktop/data/')

How can I select the name with which the file is saved?
Can be the name defined at the moment of the download?

I meant something like this:

For name in names:
    download_button = driver.find_element_by_xpath('//*[@id="content"]/div[2]/div[2]/section[1]/div[2]/ul/li[14]/div/ul/li[{}]/a'.format(name))
    download_button.click()
    save_file_as(name)

Answers:

You don’t have control over the download file naming through selenium.

What you can do is to use a directory watcher/observer to detect when the file is downloaded and then rename it accordingly. Please see this answer containing more follow-up details.

Answered By: alecxe
import os,glob

def get_last_filename_and_rename(save_folder, new_filename):
    files = glob.glob(save_folder + '/*')
    max_file = max(files, key=os.path.getctime)
    filename = max_file.split("/")[-1].split(".")[0]
    new_path = max_file.replace(filename, new_filename)
    os.rename(max_file, new_path)
    return new_path

Just a code sample. Find and rename the last downloaded file. * new_filename does not include an extension, it is inherited from the original file.

Answered By: Alex Glinsky
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.