Kristen Funk
Kristen Funk

Reputation: 53

How to change the download location using python and selenium webdriver

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 to PATH and just used

driver = webdriver.Chrome(chrome_options=options)

but that gives me this:

[Errno 2] No such file or directory. 

What am I doing wrong? Thanks!

Upvotes: 5

Views: 14095

Answers (3)

rajat prakash
rajat prakash

Reputation: 197

Try this, it will work smoothly

import webdriver
chrome_options = webdriver.ChromeOptions()

prefs = {'download.default_directory' : 'path for your folder that you want'}
chrome_options.add_experimental_option('prefs', prefs)

driver = webdriver.Chrome(chrome_options=chrome_options)

Upvotes: 2

Chandella07
Chandella07

Reputation: 2117

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")

Upvotes: 3

iamsankalp89
iamsankalp89

Reputation: 4739

Try this code it works for me, just create a profile for chrome and define the download location for the tests

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("download.default_directory=D:/Sele_Downloads")

driver = webdriver.Chrome(chrome_options=options)
driver.get("https://jpwebsite.harriscountytx.gov/PublicExtracts/search.jsp")

Upvotes: -1

Related Questions