Unable to change default download directory for google chrome while downloading using Selenium – python

Question:

I’m trying to download a file from a URL using Selenium in Python.
I’m trying to change the default download directory using Options

The script runs without any error message, but the download directory doesn’t get updated.
I’m using a jupyter notebook on Windows.
This is what my code looks like:

options = Options()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option("prefs", {
  "download.default_directory": r"C:Usersdanish.zahidDownloadspubsmed\",
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
})
# specify the title of the study you want to download
study_title = "Pan-cancer single-cell landscape of tumor-infiltrating T cells"
# start the browser and navigate to the PubMed website
s = Service(r"C:Usersdanish.zahidDownloadschromedriver_win32chromedriver.exe")
browser = webdriver.Chrome(service=s, options=options)
browser.get("https://pubmed.ncbi.nlm.nih.gov/")
# find the search box, enter the study title, and submit the form
search_box = browser.find_element(By.ID, "id_term")
search_box.send_keys(study_title)
search_box.send_keys(Keys.RETURN)
# find the save button to and click it
save_button = browser.find_element(By.XPATH, "//*[@id='save-results-panel-trigger']")
save_button.click()
# Select Pubmed from drop down
dropdownlist  = browser.find_element(By.ID, "save-action-format")
select_dropdown = Select(dropdownlist)
select_dropdown.select_by_visible_text("PubMed")
download_file = browser.find_element(By.XPATH, "//*[@id='save-action-panel-form']/div[2]/button[1]")
download_file.click()
browser.quit()

Went through a lot of possible solutions online but cant seem to make it work what am i doing wrong here.

Asked By: Danish Zahid Malik

||

Answers:

First modify your add_experimental_option:

options.add_experimental_option("prefs", {
  "download.default_directory": os.path.abspath(r"C:Usersdanish.zahidDownloadspubsmed"),
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
})

for adding wait time also, you can use time.sleep(10) after download_file.click()

Answered By: Phoenix

Here is the solution, my friend

import time
from selenium.webdriver import ChromeOptions, Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

options = ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option("prefs", {
  "download.default_directory": "C:\Users\PC\OneDrive\Documents\",
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
})
# specify the title of the study you want to download
study_title = "Pan-cancer single-cell landscape of tumor-infiltrating T cells"
# start the browser and navigate to the PubMed website

browser = Chrome(options=options)
browser.get("https://pubmed.ncbi.nlm.nih.gov/")
# find the search box, enter the study title, and submit the form
search_box = browser.find_element(By.ID, "id_term")
search_box.send_keys(study_title)
search_box.send_keys(Keys.RETURN)
# # find the save button to and click it
save_button = browser.find_element(By.XPATH, "//*[@id='save-results-panel-trigger']")
save_button.click()
# # Select Pubmed from drop-down
dropdownlist  = browser.find_element(By.ID, "save-action-format")
dropdownlist.find_element(By.CSS_SELECTOR, 'option[value="pmid"]').click()
download_file = browser.find_element(By.XPATH, "//*[@id='save-action-panel-form']/div[2]/button[1]")
download_file.click()
time.sleep(2)

As you can see, the target file "pmid-34914499.txt" will get downloaded on the mentioned path "C:UsersPCOneDriveDocuments", which is inside the Documents folder.

Answered By: Ajeet Verma