1

Hello to stack overflow community. I've been looking for a solution to permanently change the default download path of browsers (chrome, firefox and edge) to the one i specify in my python script. So far this python script is tested with chrome, which does not provide the desired results!

from selenium import webdriver

chromeOptions = webdriver.ChromeOptions()
prefs = {"download.default_directory" : "/some/path"}
chromeOptions.add_experimental_option("prefs",prefs)
chromedriver = "./chromedriver"
driver = webdriver.Chrome(executable_path=chromedriver, chrome_options=chromeOptions)

What essentially this script does is, open a new chrome window and change its download path to one specified in the script! Changes made are, just limited to this opened window that are discarded on window closing.

It seems to be creating some sort of session which certainly does not affect the original/default window's settings!

I might not have used the proper words/terms to describe the problem, please let me know if my problem is not clear enough!

Edit: In my case, use of selenium does not matter/necessary, what i want is a more generalized solution(plug and play) that on running(script) changes the path and does not need any setup of profile paths or binaries which is the case with selenium!

Faizan Mustafa
  • 371
  • 5
  • 19

2 Answers2

0

You can load default profiles and with all your configurations.
You can find detailed instructions how to load default profile to Chrome here:

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)
Sers
  • 12,047
  • 2
  • 12
  • 31
  • Thanks for the answer, but i want a more generalized solution(not restricted to selenium) with no setup of binaries/executeables and profile paths... – Faizan Mustafa Oct 26 '19 at 17:33
  • What do you want completely unclear. Maybe you should remove Selenium tag and rephrase question – Sers Oct 26 '19 at 17:40
  • 1
    Sorry for the ambiguity. I have edited my question. Basically all i want is a python script whose only purpose is to change the download paths, nothing less nothing more. And that script if given to somebody, he does not have to replace/setup any thing in the script. Plug and Play like thing... – Faizan Mustafa Oct 26 '19 at 17:52
0

I have reproduced the problem on my machine, it seems that by changing the ChromeOptions and the prefs option using selenium, we could only change the selenium opened window setting, instead of the Chrome browser Default setting.

To permanently change the default download path of browsers (Chrome, Firefox and edge) to the one I specify in my python script. I suggest you could directly change the download path from the registry (without using the selenium webdriver).

For example, the chrome Registry path as below:

enter image description here

Then, we could use the following code to change the registry key value.

import winreg
key = winreg.CreateKey(winreg.HKEY_CURRENT_USER, 'Software\Policies\Google\Chrome')
winreg.SetValueEx(key, 'DownloadDirectory', 0, winreg.REG_SZ, 'D:\TempData')
key.Close()

The result like this:

enter image description here

For the Microsoft Edge browser, the download folder Registry path is:

Computer\HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Storage\microsoft.microsoftedge_8wekyb3d8bbwe\MicrosoftEdge\Main (set the Default Download Directory key value, like this)

or set the user shell folders:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders

enter image description here

Then, we can use the following code to modify the registry key value.

import winreg
key = winreg.CreateKey(winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders')
winreg.SetValueEx(key,'{374DE290-123F-4565-9164-39C4925E467B}', 0, winreg.REG_SZ, 'D:\TempData')
key.Close()

[Note] When using this method to modify the browser setting, it is better have the Admin permission.

Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • Thanks for the reply, What about linux and Mac operating systems? – Faizan Mustafa Oct 28 '19 at 12:15
  • we could also use this method, but we need to find the registry key location. About the Microsoft Edge browser, try to use the above path or check this path `Computer\HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Storage\microsoft.microsoftedge_8wekyb3d8bbwe\MicrosoftEdge\Main` (set the `Default Download Directory` key value, [like this](https://i.stack.imgur.com/8YTT1.png)). For the Chrome browser registry, please check the [DefaultDownloadDirectory](https://www.chromium.org/administrators/policy-list-3#DefaultDownloadDirectory). – Zhi Lv Oct 29 '19 at 07:03