Downloading with chrome headless and selenium Downloading with chrome headless and selenium python python

Downloading with chrome headless and selenium


Yes, it's a "feature", for security. As mentioned before here is the bug discussion: https://bugs.chromium.org/p/chromium/issues/detail?id=696481

Support was added in chrome version 62.0.3196.0 or above to enable downloading.

Here is a python implementation. I had to add the command to the chromedriver commands. I will try to submit a PR so it is included in the library in the future.

def enable_download_in_headless_chrome(self, driver, download_dir):    # add missing support for chrome "send_command"  to selenium webdriver    driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')    params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}    command_result = driver.execute("send_command", params)

For reference here is a little repo to demonstrate how to use this:https://github.com/shawnbutton/PythonHeadlessChrome

update 2020-05-01 There have been comments saying this is not working anymore. Given this patch is now over a year old it's quite possible they have changed the underlying library.


Here's a working example for Python based on Shawn Button's answer. I've tested this with Chromium 68.0.3440.75 & chromedriver 2.38

from selenium import webdriverfrom selenium.webdriver.chrome.options import Optionschrome_options = Options()chrome_options.add_experimental_option("prefs", {  "download.default_directory": "/path/to/download/dir",  "download.prompt_for_download": False,})chrome_options.add_argument("--headless")driver = webdriver.Chrome(chrome_options=chrome_options)driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': "/path/to/download/dir"}}command_result = driver.execute("send_command", params)driver.get('http://download-page.url/')driver.find_element_by_css_selector("#download_link").click()


This is a feature of Chrome to prevent from software to download files to your computer. There is a workaround though. Read more about it here.

What you need to do is enable it via DevTools, Something like that:

async function setDownload () {  const client = await CDP({tab: 'ws://localhost:9222/devtools/browser'});  const info =  await client.send('Browser.setDownloadBehavior', {behavior : "allow", downloadPath: "/tmp/"});  await client.close();}

This is the solution some one gave in the mentioned topic. Here is his comment.