Python 中的 Selenium Webdriver - Chrome 首选项中的文件下载目录更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18026391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:45:35  来源:igfitidea点击:

Selenium Webdriver in Python - files download directory change in Chrome preferences

pythonpython-2.7selenium-webdriverselenium-chromedriver

提问by Parzival

I'm using Selenium Webdriver (in Python) to automate the downloading of thousands of files. I want to set Chrome's download folder programmatically. After reading this, I tried this:

我正在使用 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)

No good. Downloads still go to the default download folder ("/Users/thiagomarzagao/Downloads").

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

Any thoughts?

有什么想法吗?

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

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

回答by R Dub

I think you also need

我想你也需要

"directory_upgrade": true

Using the dictionary directly in a Chrome 'Prefrences' file, on a local windows install of chrome Version 28.0.1500.95 m, with the following download options:

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

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

I get the default location, versus the desktop. When I change it to this:

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

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

I get the desktop location.

我得到桌面位置。

Try the following:

请尝试以下操作:

desired_caps = {'prefs': {'download': {'default_directory': '/Users/thiagomarzagao/Desktop/downloaded_files/', "directory_upgrade": true, "extensions_to_open": ""}}}

回答by MervS

The following worked for me:

以下对我有用:

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, chrome_options=chromeOptions)

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

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

回答by shakaran

I try all the anwsers in this question, but it doesn't work for my in Ubuntu 16.10. So I add the change with os.environ for the variable XDG_DOWNLOAD_DIR. Which doesn't work, but I think that it helps.

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

That is:

那是:

os.environ['XDG_DOWNLOAD_DIR'] = default_download_directory

The really change that works perfectly for meis setup the download folder via the command xdg-user-dirs-updatethrough a system call in execution time:

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

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

So, all my code related to setup the download dir is the following:

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

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)

回答by yvesva

If anyone is still having trouble and the above solutions didn't work, I found adding a following slash ('\') to my download path.

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

Mine looked like this:

我的看起来像这样:

    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)

回答by Camilo Fosco

For anybody still wondering why their implementation doesn't work: You have to put the FULL PATH for it to work. e.g. '/Users/you/dlfolder' won't work, while 'C:/Users/you/dlfolder' will.

对于仍然想知道为什么他们的实现不起作用的人:您必须设置完整路径才能使其工作。例如,“/Users/you/dlfolder”将不起作用,而“C:/Users/you/dlfolder”将起作用。