42

我正在使用 Selenium Webdriver(在 Python 中)来自动下载数千个文件。我想以编程方式设置 Chrome 的下载文件夹。读完这篇文章后,我尝试了这个:

chromepath = '/Users/thiagomarzagao/Desktop/searchcode/chromedriver'
desired_caps = {'prefs': {'download': {'default_directory': '/Users/thiagomarzagao/Desktop/downloaded_files/'}}}
driver = webdriver.Chrome(executable_path = chromepath, desired_capabilities = desired_caps)

不好。下载仍会转到默认下载文件夹(“/Users/thiagomarzagao/Downloads”)。

有什么想法吗?

(Python 2.7.5、Selenium 2.2.0、Chromedriver 2.1.210398、Mac OS X 10.6.8)

4

8 回答 8

78

以下对我有用:

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

来源:https ://sites.google.com/a/chromium.org/chromedriver/capabilities

于 2013-09-26T09:46:09.367 回答
34

如果有人仍然遇到问题并且上述解决方案不起作用,我发现在我的下载路径中添加了一个斜杠 ('\')。

我的看起来像这样:

    if browser == 'chrome':
        options = webdriver.ChromeOptions()
        options.add_argument("--start-maximized")
        prefs = {"profile.default_content_settings.popups": 0,
                 "download.default_directory": r"C:\Users\user_dir\Desktop\\", # IMPORTANT - ENDING SLASH V IMPORTANT
                 "directory_upgrade": True}
        options.add_experimental_option("prefs", prefs)
        return webdriver.Chrome(executable_path=Base.chromedriver_dir, chrome_options=options)
于 2017-03-22T05:41:57.340 回答
7

我想你也需要

"directory_upgrade": true

在 Chrome 版本 28.0.1500.95 m 的本地 Windows 安装上,直接在 Chrome 的“首选项”文件中使用字典,并具有以下下载选项:

   "download": {
      "default_directory": "C:\\Users\\rdub\\Desktop",
      "extensions_to_open": ""
   },

我得到默认位置,而不是桌面。当我将其更改为:

   "download": {
      "default_directory": "C:\\Users\\rdub\\Desktop",
      "directory_upgrade": true,
      "extensions_to_open": ""
   },

我得到桌面位置。

尝试以下操作:

desired_caps = {'prefs': {'download': {'default_directory': '/Users/thiagomarzagao/Desktop/downloaded_files/', "directory_upgrade": true, "extensions_to_open": ""}}}
于 2013-08-02T22:23:15.147 回答
4
# -*- coding: utf-8 -*-
from selenium import webdriver 
from selenium.webdriver.chrome.options import Options
import time
temp_directory = ""
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--verbose')
chrome_options.add_experimental_option("prefs", {
        "download.default_directory": "<path to the folder of download>",
        "download.prompt_for_download": False,
        "download.directory_upgrade": True,
        "safebrowsing_for_trusted_sources_enabled": False,
        "safebrowsing.enabled": False
})
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-software-rasterizer')
url = "https://www.thinkbroadband.com/download"
driver = webdriver.Chrome(executable_path = './chromedriver' ,chrome_options = chrome_options)
driver.get(url)
time.sleep(5)
driver.find_element_by_css_selector("div.module:nth-child(8) > p:nth-child(1) > a:nth-child(1) > img:nth-child(1)").click()
于 2021-04-07T20:50:50.480 回答
3

我尝试了这个问题中的所有答案,但它不适用于我的Ubuntu 16.10。因此,我使用 os.environ 为变量XDG_DOWNLOAD_DIR添加了更改。这不起作用,但我认为它有帮助。

那是:

os.environ['XDG_DOWNLOAD_DIR'] = default_download_directory

真正适合我的更改是通过命令xdg-user-dirs-update在执行时通过系统调用设置下载文件夹:

os.system("xdg-user-dirs-update --set DOWNLOAD " + default_download_directory)

因此,我所有与设置下载目录相关的代码如下:

import os
from selenium import webdriver

default_download_directory = str(os.path.dirname(os.path.abspath(__file__))) + "/download"

os.environ['XDG_DOWNLOAD_DIR'] = default_download_directory

os.system("xdg-user-dirs-update --set DOWNLOAD " + default_download_directory)

desired_caps = {
    'prefs': {
            'download': {
                'default_directory': str(os.path.dirname(os.path.abspath(__file__))) + "/download", 
                "directory_upgrade": "true", 
                "extensions_to_open": ""
                }
              }
        }

options = webdriver.ChromeOptions() 
options.add_argument("download.default_directory=" + str(os.path.dirname(os.path.abspath(__file__))) + "/download")

browser = webdriver.Chrome(chrome_options=options, desired_capabilities=desired_caps)
于 2017-01-29T12:20:44.803 回答
2

将变量传递给“download.default_directory”

将目录路径存储在变量中并将变量传递给“download.default_directory”

注意: 同一位置的 .py 文件和文件夹“PDF_Folder”均应下载到文件夹“PDF_Folder”中

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");
于 2020-09-03T16:04:35.163 回答
2

对于仍然想知道为什么他们的实现不起作用的任何人:您必须输入完整路径才能使其正常工作。例如 '/Users/you/dlfolder' 不起作用,而 'C:/Users/you/dlfolder' 会。

于 2018-02-24T21:32:35.427 回答
0

这似乎对我有用

chrome_options = webdriver.ChromeOptions()
settings = {
       "recentDestinations": [{
            "id": "Save as PDF",
            "origin": "local",
            "account": "",
        }],
        "selectedDestinationId": "Save as PDF",
        "version": 2,

    }



prefs = {'savefile.default_directory': '/Users/gregreynders/PycharmProjects/Webscraper'}
chrome_options.add_experimental_option('prefs', prefs)
chrome_options.add_argument('--kiosk-printing')


driver = webdriver.Chrome(chrome_options=chrome_options , executable_path="/Applications/chrome/chromedriver" )
driver.get("https://google.com")
driver.execute_script('window.print();')
driver.quit()
于 2021-07-18T20:05:14.397 回答