2

I tried the following code to change the default download location of chrome. But still, the file is downloaded at "Downloads".

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

ChromeOptions=Options()
ChromeOptions.add_experimental_option("prefs", {"download.default_directory":"C:\\Users\Elite\Desktop"})
driver=webdriver.Chrome(executable_path='C:\Webdrivers\chromedriver.exe',chrome_options=ChromeOptions)
driver.get("url/website")
driver.find_element_by_xpath('xpath of download file').click()

I also tried this:

options = webdriver.ChromeOptions()
options.add_argument('download.default_directory=C:/Users/Elite/Desktop')
driver=webdriver.Chrome(options=options)

Any help would be appreciated !!!!

Abanish Tiwari
  • 167
  • 1
  • 2
  • 14
  • Please take your time and search before posting: https://stackoverflow.com/a/18440478/13982891 and also this answer is useful: https://stackoverflow.com/a/52285731/13982891 – Ali Adhami Aug 22 '20 at 08:50
  • @AliAdhami I am new to this. I might have tried the solutions all i can reach. But is not working for me. Can you suggest the changes to be made here. – Abanish Tiwari Aug 22 '20 at 08:59

1 Answers1

2

Hi this is the Correct way to do it

Code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = webdriver.ChromeOptions()

options.add_experimental_option("prefs", {
  "download.default_directory": r"C:\Users\Elite\Desktop",
  "download.directory_upgrade":True,
  "safebrowsing.enabled":True,
  "download.prompt_for_download":False,
})

driver=webdriver.Chrome(executable_path="C:\Webdrivers\chromedriver.exe",options=options)

driver.get("url/website")
driver.find_element_by_xpath('xpath of download file').click()
Abanish Tiwari
  • 167
  • 1
  • 2
  • 14
Ali Adhami
  • 182
  • 4
  • Thank you for the answer. It really changed the download location in the settings. But the problem is: somehow it reconfigured my chrome and I am not able to download from the chrome though can be done with the firefox browser. – Abanish Tiwari Aug 22 '20 at 12:32
  • The absolute path "/" needs to be changed – Abanish Tiwari Aug 22 '20 at 13:15
  • When I used "download.prompt_for_download":True it reasigns the download location to Downloads. How can I change it – Abanish Tiwari Aug 22 '20 at 13:16
  • @AbanishTiwari on using this answer, the clicks that were inducing the download of files is not downloading them anymore. It gives a download error. Any help? – Soumya Pandey Sep 28 '21 at 06:24