Reputation: 3828
I am downloading several different data sets and would like each file (or set) to download to a specific folder. I have learned how to change the download directories at these page:
setting Chrome preferences w/ Selenium Webdriver in Python
Change the default chrome download folder webdriver C#
The problem is these methods only allow me to change the download directory when I open the webdriver. It takes a while to get to the download page so doing this is an ineffective solution. I've tried set preferences but I'm working with selenium webdriver and chrome in python and I have not been able to find anything on SO or in the python help. Even switching the window handle on a new driver won't work because it cannot grab another driver's already open window.
The link for the download site is customized so can't copy and paste into a new driver either. So far I've been using the os. module to get the name of each new file coming in but even this is unreliable because of varying download times.
If anyone has any idea on how to change the default settings to a webdriver while the webdriver is running that would be great. Thanks!
Upvotes: 7
Views: 7192
Reputation: 837
I've looking up for something but can't find any useful for the same. I think the best option is declare a temporal download path and after that, move the file that you downloaded to your desire path using OS library.
Upvotes: 0
Reputation: 1
I used the chromeOptions Experimental method.
##set download directory
chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory": "your directory path"}
chromeOptions.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver',
chrome_options=chromeOptions)
I have my chromedriver in /usr/bin in this example. The only disadvantage is you have to keep your chromedriver up to date in accordance with browser updates. So when you update your browser you have to update the chromedriver.
This works for me.
Upvotes: -1
Reputation: 1184
In the past, I have solved this by downloading to a temp folder and then renaming the file to the appropriate folder with something along the line of this:
def move_to_download_folder(downloadPath, newFileName, fileExtension):
got_file = False
## Grab current file name.
while got_file = False:
try:
currentFile = glob.glob(DOWNLOAD_PATH+"*"+fileExtension)
got_file = True
except:
print "File has not finished downloading"
time.sleep(20)
## Create new file name
fileDestination = downloadPath+newFileName+fileExtension
os.rename(currentFile, fileDestination)
return
## Click element to download file
inputElement=driver.find_element_by_xpath("{xpath here}").click()
move_to_download_folder(downloadPath, newFileName, fileExtension)
Upvotes: 5