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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With