Abanish Tiwari
Abanish Tiwari

Reputation: 167

How can I download a file in the desired location with chrome using Python?

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 !!!!

Upvotes: 2

Views: 487

Answers (1)

Ali Adhami
Ali Adhami

Reputation: 202

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

Upvotes: 3

Related Questions