简体   繁体   中英

Python Selenium with Chrome, how to download to a specified folder with specified file name

The website has a download button and in python I can do

button.click()

to get the file downloaded to the Chrome download folder with a filename specified by the website.

Is there a way to change the target folder and filename, on Windows?

Try with:

    download_dir = "/yourDownloadPath/"
    chrome_options = webdriver.ChromeOptions()
    preferences = {"download.default_directory": download_dir ,
                   "directory_upgrade": True,
                   "safebrowsing.enabled": True }
    chrome_options.add_experimental_option("prefs", preferences)
    driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=r'/pathTo/chromedriver')
    driver.get("urlfiletodownload");

You can create a profile for chrome and define the download location for the tests. Here is an example:

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

options = webdriver.ChromeOptions() 
options.add_argument("download.default_directory=C:/Downloads")

driver = webdriver.Chrome(chrome_options=options)

Works !!!

Store the dir path in variable and pass the variable to "download.default_directory"

exepath = sys.arg[0]
# get the path from the .py file
Dir_path = os.path.dirname(os.path.abspath(exepath))
# get the path of "PDF_Folder" directory
Download_dir = Dir_path+"\\PDF_Folder\\"

preferences = {"download.default_directory": Download_dir , # pass the variable
                   "download.prompt_for_download": False,
                   "directory_upgrade": True,
                   "safebrowsing.enabled": True }
chrome_options.add_experimental_option("prefs", preferences)
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=r'/pathTo/chromedriver')
driver.get("urlfiletodownload");

After wasting some time, i found out, that the recommended solution did NOT work for me:

options.add_argument("download.default_directory=C:/MyDownloadPath")

This is a code snippet below that worked for me. The chromedriver.exe was in the same folder as my python script and i wanted to download also to the same folder. You won't need the executable_path parameter if the chromedriver is in your PATH and can be found by selenium.

import os
from selenium import webdriver

localdir = os.path.dirname(os.path.realpath(__file__))
chromeOptions = webdriver.ChromeOptions()
prefs = { "download.default_directory" : localdir }
chromeOptions.add_experimental_option("prefs", prefs)
exe_path = os.path.join(localdir, 'chromedriver.exe')
with webdriver.Chrome(executable_path=exe_path, options=chromeOptions) as driver:
    # do your chrome download stuff here:
    driver.get(link)

My System:

Windows 10
Python 3.7.6
Chrome 80.0.3987.132
ChromeDriver 80.0.3987.106
Pip Module: Selenium 3.141.0
Date: March 2020

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM