Shubham Goyal
Shubham Goyal

Reputation: 667

Downloading a file at a specified location through python and selenium using Chrome driver

I am trying to automatically download some links through selenium's click functionality and I am using a chrome webdriver and python as the programming language. How can I select the download directory through the python program so that it does not get downloaded in the default Downloads directory. I found a solution for firefox but there the download dialog keeps popping up every time it clicks on the link which does not happen in Chrome.

Upvotes: 55

Views: 134545

Answers (12)

Yanov
Yanov

Reputation: 674

It's for relative path:

download_directory = os.path.abspath('./tmp')
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : download_directory}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(options=chrome_options)

Upvotes: 0

Matthias Chin
Matthias Chin

Reputation: 71

Update 2022:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()

prefs = {"download.default_directory" : "C:\YourDirectory\Folder"}

options.add_experimental_option("prefs", prefs)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

Upvotes: 1

Abhishake Gupta
Abhishake Gupta

Reputation: 3170

Below code snippet holds good for Windows/linux/MacOs distro:

    downloadDir = f"{os.getcwd()}//downloads//"
    # Make sure path exists.
    Path(downloadDir).mkdir(parents=True, exist_ok=True)
    
    # Set Preferences.
    preferences = {"download.default_directory": downloadDir,
                   "download.prompt_for_download": False,
                   "directory_upgrade": True,
                   "safebrowsing.enabled": True}

    chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_argument("--window-size=1480x560")
    chromeOptions.add_experimental_option("prefs", preferences)

    driver = webdriver.Chrome(DRIVER_PATH, options=chromeOptions)
    driver.get(url)
    time.sleep(10)
    driver.close()

Upvotes: 0

Alireza M
Alireza M

Reputation: 11

I see that many people have the same problem, just add the backslash at the end

op = webdriver.ChromeOptions()
prefs = {'download.default_directory' : 'C:\\Users\\SAJComputer\\PycharmProjects\\robot-test\\'}
op.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path=driver_path , options=op)

Upvotes: 1

Rajesh Kumar Sahoo
Rajesh Kumar Sahoo

Reputation: 463

the exact problem I also have faced while trying to do exactly same what you want to :)

For chrome:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
prefs = {"profile.default_content_settings.popups": 0,
             "download.default_directory": 
                        r"C:\Users\user_dir\Desktop\\",#IMPORTANT - ENDING SLASH V IMPORTANT
             "directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
browser=webdriver.Chrome(<chromdriver.exe path>, options=options)

For Firefox: follow this blog for the answer: https://srirajeshsahoo.wordpress.com/2018/07/26/how-to-bypass-pop-up-during-download-in-firefox-using-selenium-and-python/

The blog says all about the pop-up and downloads dir and how to do

Upvotes: 17

hoju
hoju

Reputation: 29452

I found the accepted solution didn't work, however this slight change did:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : '/path/to/dir'}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

Upvotes: 136

user9763662
user9763662

Reputation: 51

Using prefs solved my problem

path = os.path.dirname(os.path.abspath(__file__))
prefs = {"download.default_directory":path}
options = Options()
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome('../harveston/chromedriver.exe',options = options)

Upvotes: 5

Shubham Jain
Shubham Jain

Reputation: 17553

This is non code level solution with no chrome profiling/options settings.

If you are using script only on your local machine then use this solution

Click on Menu -> Setting -> Show advanced settings... -> Downloads

Now uncheck

Ask where to save each file before downloading

enter image description here

Hope it will help you :)

Upvotes: -7

CodeWalker
CodeWalker

Reputation: 2368

This worked for me on Chrome v81.0.4044.138

preferences = {
                "profile.default_content_settings.popups": 0,
                "download.default_directory": os.getcwd() + os.path.sep,
                "directory_upgrade": True
            }

chrome_options.add_experimental_option('prefs', preferences)
browser = webdriver.Chrome(executable_path="/usr/bin/chromedriver", options=chrome_options)

Upvotes: 4

Alex Montoya
Alex Montoya

Reputation: 5099

If you are using linux distribution

Use this code

prefs = {'download.prompt_for_download': False,
         'download.directory_upgrade': True,
         'safebrowsing.enabled': False,
         'safebrowsing.disable_download_protection': True}

options.add_argument('--headless')
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome('chromedriver.exe', chrome_options=options)
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
driver.desired_capabilities['browserName'] = 'ur mum'
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': r'C:\chickenbutt'}}
driver.execute("send_command", params)

Upvotes: 1

vsnahar
vsnahar

Reputation: 77

To provide download directory and chrome's diver executable path use the following code.

from selenium import webdriver
options = webdriver.ChromeOptions() 
options.add_argument("download.default_directory=C:/Your_Directory")
driver = webdriver.Chrome(options=options ,executable_path='C:/chromedriver')

change the path in your code accordingly.

Upvotes: 0

Sarunas Urbelis
Sarunas Urbelis

Reputation: 587

Update 2018:

Its not valid Chrome command line switch, see the source code use hoju answer below to set the Preferences.

Original:

You can create a profile for chrome and define the download location for the tests. Here is an example:

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("download.default_directory=C:/Downloads")

driver = webdriver.Chrome(chrome_options=options)

Upvotes: 27

Related Questions