How to change the download location using python and selenium webdriver
11,046
You can use timestamp to create new directory. Also use preference dictionary for chrome options with prompt_for_download
and directory_upgrade
params. try below example:
from selenium import webdriver
import time
timestr = time.strftime("%Y%m%d-%H%M%S")
options = webdriver.ChromeOptions()
prefs = {
"download.default_directory": r"C:\Users\XXXX\downdir\stamp"+timestr,
"download.prompt_for_download": False,
"download.directory_upgrade": True
}
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=options)
driver.get("https://jpwebsite.harriscountytx.gov/PublicExtracts/search.jsp")
Author by
Kristen Funk
Updated on June 04, 2022Comments
-
Kristen Funk almost 2 years
I've written the code to download files for each month in a range of years for every precinct and place. However, since I can't change the name of the files through selenium, I was hoping to download each place's files into a separate folder. Here's my code
options = webdriver.ChromeOptions() options.add_argument('download.default_directory=/Users/name/Downloads/' + p) driver = webdriver.Chrome(chrome_options=options, executable_path="/Users/name/Downloads/chromedriver") driver.get("https://jpwebsite.harriscountytx.gov/PublicExtracts/search.jsp")
where
p
is the id of a particular precinct and place. Unfortunately the files are downloaded to/Users/name/Downloads
. I've added chromedriver toPATH
and just useddriver = webdriver.Chrome(chrome_options=options)
but that gives me this:
[Errno 2] No such file or directory.
What am I doing wrong? Thanks!
-
curious_nustian over 4 yearsWorks fine, can you explain directory upgrade ?
-
Chandella07 over 4 yearsIt is related to change in download dir by means of an upgrade. see the complete documentation on this link : cs.chromium.org/chromium/src/chrome/common/pref_names.cc