I want to download a file from a website with splinter where the file is generated after a click on a button and provided by a random URL. Clicking the button makes the file save dialog of the browser appear.
I can find and click the button with
exportButton = browser.find_by_id("exportButton")
exportButton.click()
But how to save the file with a certain name?
Because the URL is random, never in the source code of the page and the Button is a button that triggers JS and not a link, the methods for file downloading with splinter by sending a request with an URL and deal with the response doesn't work. I can't provide an URL. Clicking the button which sends probably an Ajax request to the server which responds with the file is the only way to get it. I use the Chrome web driver in headless mode.
So I don't know of a way to change the name before download. But I have been able to download the file automatically then change the file name afterwards.
import splinter
from selenium import webdriver
from shutil import copyfile
options = webdriver.ChromeOptions()
prefs = {
"download.default_directory" : "C:/Users/joshuaclew/Downloads/",
"download.directory_upgrade": "true",
"download.prompt_for_download": "false",
"disable-popup-blocking": "true"
}
chrome_options = webdriver.ChromeOptions()
options.add_experimental_option("prefs", prefs)
chrome_options.add_argument("--disable-infobars")
browser = splinter.Browser('chrome', options=chrome_options)
browser.driver.maximize_window()
browser.visit('http://www.someurlhere.com')
exportButton = browser.find_by_id("exportButton")
exportButton.click()
download_path = 'C:/Users/joshuaclew/Downloads/'
old_file_name = download_path+'old_file_name'
new_file_name = download_path+'new_file_name'
copyfile(old_file_name, new_file_name)