在Python中设置Chrome首选项w/ Selenium Webdriver。

时间:2021-11-30 09:23:37

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

没有好。下载仍然会进入默认的下载文件夹(“/用户/thiagomarzagao/下载”)。

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)

5 个解决方案

#1


35  

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

#2


4  

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


3  

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的“prefrent”文件中直接使用字典,在本地windows安装的Chrome版本28.0.1500.95 m,有以下下载选项:

   "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": ""}}}

#4


1  

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.

在这个问题中,我尝试了所有的anwsers,但是在Ubuntu 16.10中它并不适用。所以我把改变加上操作系统。环境变量XDG_DOWNLOAD_DIR。这行不通,但我认为这很有帮助。

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:

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

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

因此,我所有有关安装下载目录的代码如下:从selenium导入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)

#5


0  

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.

对于那些仍然想知道为什么它们的实现不起作用的人来说:你必须为它提供完整的工作路径。如。“/用户/你/dlfolder”无法工作,而“C:/用户/你/dlfolder”将会。

#1


35  

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

#2


4  

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


3  

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的“prefrent”文件中直接使用字典,在本地windows安装的Chrome版本28.0.1500.95 m,有以下下载选项:

   "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": ""}}}

#4


1  

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.

在这个问题中,我尝试了所有的anwsers,但是在Ubuntu 16.10中它并不适用。所以我把改变加上操作系统。环境变量XDG_DOWNLOAD_DIR。这行不通,但我认为这很有帮助。

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:

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

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

因此,我所有有关安装下载目录的代码如下:从selenium导入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)

#5


0  

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.

对于那些仍然想知道为什么它们的实现不起作用的人来说:你必须为它提供完整的工作路径。如。“/用户/你/dlfolder”无法工作,而“C:/用户/你/dlfolder”将会。