setting Chrome preferences w/ Selenium Webdriver i

2019-01-11 13:54发布

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:

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").

Any thoughts?

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

5条回答
三岁会撩人
2楼-- · 2019-01-11 14:07

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)
查看更多
看我几分像从前
3楼-- · 2019-01-11 14:07

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.

查看更多
仙女界的扛把子
4楼-- · 2019-01-11 14:10

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.

That is:

os.environ['XDG_DOWNLOAD_DIR'] = default_download_directory

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

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)
查看更多
倾城 Initia
5楼-- · 2019-01-11 14:24

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:

   "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": ""}}}
查看更多
仙女界的扛把子
6楼-- · 2019-01-11 14:28

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

查看更多
登录 后发表回答