Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 965
Shubham Goyal Avatar asked Feb 11 '16 05:02

Shubham Goyal


People also ask

How do I set a Chrome driver path?

Any of these steps should do the trick: include the ChromeDriver location in your PATH environment variable. (Java only) specify its location via the webdriver.chrome.driver system property (see sample below) (Python only) include the path to ChromeDriver when instantiating webdriver.Chrome (see sample below)


2 Answers

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) 
like image 121
hoju Avatar answered Oct 02 '22 20:10

hoju


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) 
like image 27
Sarunas Urbelis Avatar answered Oct 02 '22 18:10

Sarunas Urbelis