如何在selenium中使用chrome webdriver下载python中的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46937319/
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 17:56:11  来源:igfitidea点击:

How to use chrome webdriver in selenium to download files in python?

pythongoogle-chromeselenium

提问by Alex

Based on the posts hereand hereI am trying to use a chrome webdriver in selenium to be able to download a file. Here is the code so far

基于此处此处的帖子,我尝试在 selenium 中使用 chrome webdriver 来下载文件。这是到目前为止的代码

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

chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_experimental_option("profile.default_content_settings.popups", 0)
chrome_options.add_experimental_option("download.prompt_for_download", "false")
chrome_options.add_experimental_option("download.default_directory", "/tmp")

driver = webdriver.Chrome(chrome_options=chrome_options)

But this alone results in the following error:

但仅此一项就会导致以下错误:

WebDriverException: Message: unknown error: cannot parse capability: chromeOptions
from unknown error: unrecognized chrome option: download.default_directory
  (Driver info: chromedriver=2.24.417424 (c5c5ea873213ee72e3d0929b47482681555340c3),platform=Linux 4.10.0-37-generic x86_64)

So how to fix this? Do I have to use this 'capability' thing? If so, how exactly?

那么如何解决这个问题呢?我必须使用这种“能力”吗?如果是这样,具体如何?

回答by Satish

Try this. Executed on windows

尝试这个。在windows上执行

(How to control the download of files with Selenium Python bindings in Chrome)

如何在 Chrome 中使用 Selenium Python 绑定控制文件的下载

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

options = Options()
options.add_experimental_option("prefs", {
  "download.default_directory": r"C:\Users\xxx\downloads\Test",
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
  "safebrowsing.enabled": True
})

回答by talebook

Some tips:

一些技巧:

  1. chromium and chromedriver should have same version.

    Typically chromium package should have chromedriver inside, you can find it in the install dir. If you are using ubuntu/debian, execute dpkg -L chromium-chromedriver.

  2. Have a correct Chrome preference config.

    as Satish said, use options.add_experimental_option("prefs", ...)to config selenium+chrome. But sometimes the config may change by time. The beset way to get newest and workable prefs is to check it in the chromium config dir.For example,

    • Launch a chromium in Xorg desktop
    • Change settings in menu
    • Quit chromium
    • Find out the real settings in ~/.config/chromium/Default/Preferences
    • Read it, pick out the exact options you need.
  1. 铬和 chromedriver 应该有相同的版本。

    通常chromium包里面应该有chromedriver,你可以在安装目录中找到它。如果您使用的是 ubuntu/debian,请执行dpkg -L chromium-chromedriver.

  2. 拥有正确的 Chrome 首选项配置。

    正如 Satish 所说,用于options.add_experimental_option("prefs", ...)配置 selenium+chrome。但有时配​​置可能会随着时间而改变。获得最新和可行的首选项的麻烦方法是在chromium config 目录中检查它。例如,

    • 在 Xorg 桌面上启动一个 Chromium
    • 更改菜单中的设置
    • 戒掉铬
    • 找出真实的设置 ~/.config/chromium/Default/Preferences
    • 阅读它,挑选出您需要的确切选项。

In my case, the code is:

就我而言,代码是:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

options = webdriver.ChromeOptions()
options.gpu = False
options.headless = True
options.add_experimental_option("prefs", {
    "download.default_directory" : "/data/books/chrome/",
    'profile.default_content_setting_values.automatic_downloads': 2,
    })

desired = options.to_capabilities()
desired['loggingPrefs'] = { 'performance': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=desired)

回答by Andrey Semakin

I think that the easiest way to save arbitrary file (i.e. image) using WebDriver is to execute JavaScript which will save file. No configuration required at all!

我认为使用 WebDriver 保存任意文件(即图像)的最简单方法是执行将保存文件的 JavaScript。完全不需要配置!

I use this library FileSaver.jsto save a file with desired name with ease.

我使用这个库FileSaver.js轻松保存具有所需名称的文件。

from selenium import webdriver
import requests

FILE_SAVER_MIN_JS_URL = "https://raw.githubusercontent.com/eligrey/FileSaver.js/master/dist/FileSaver.min.js"

file_saver_min_js = requests.get(FILE_SAVER_MIN_JS_URL).content

chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)

# Execute FileSaver.js in page's context
driver.execute_script(file_saver_min_js)

# Now you can use saveAs() function
download_script = f'''
    return fetch('https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=a010291124bf',
        {{
            "credentials": "same-origin",
            "headers": {{"accept":"image/webp,image/apng,image/*,*/*;q=0.8","accept-language":"en-US,en;q=0.9"}},
            "referrerPolicy": "no-referrer-when-downgrade",
            "body": null,
            "method": "GET",
            "mode": "cors"
        }}
    ).then(resp => {{
        return resp.blob();
    }}).then(blob => {{
        saveAs(blob, 'stackoverflow_logo.svg');
    }});
    '''

driver.execute_script(download_script)
# Done! Your browser has saved an SVG image!

回答by neelmeg

for chrome in mac os, the download.defaultdirectory did not work for me and fortunately savefile.default_directoryworks.

对于 mac os 中的 chrome,download.defaultdirectory 对我不起作用,幸运的是savefile.default_directory工作。

prefs = {
    "printing.print_preview_sticky_settings.appState": json.dumps(settings),
    "savefile.default_directory": "/Users/creative/python-apps",
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "download.safebrowsing.enabled": True
}

回答by Davide Patti

From your exception, you are using chromedriver=2.24.417424.

从您的例外情况来看,您正在使用chromedriver=2.24.417424.

What are the versions of Selenium and Chrome browser that are you using?

您使用的是什么版本的 Selenium 和 Chrome 浏览器?

I tried the following code with:

我尝试了以下代码:

  • Selenium 3.6.0
  • chromedriver 2.33
  • Google Chrome 62.0.3202.62 (Official Build) (64-bit)
  • 硒 3.6.0
  • 铬驱动程序 2.33
  • 谷歌浏览器 62.0.3202.62(官方版本)(64 位)

And it works:

它有效:

from selenium import webdriver

download_dir = "/pathToDownloadDir"
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");

Make sure you are using a browser that is supported by your chromedriver (from here, should be Chrome v52-54).

确保您使用的是 chromedriver 支持的浏览器(从这里开始,应该是Chrome v52-54)。