Download file through Google Chrome in headless mode Download file through Google Chrome in headless mode selenium selenium

Download file through Google Chrome in headless mode


First the solution

Minimum Prerequisites:

To download the file clicking on the element with text as Download Data within this website you can use the following solution:

  • Code Block:

    from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.chrome.options import Optionsoptions = Options()options.add_argument("--headless")options.add_argument("--window-size=1920,1080")options.add_experimental_option("excludeSwitches", ["enable-automation"])options.add_experimental_option('useAutomationExtension', False)driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe', service_args=["--log-path=./Logs/DubiousDan.log"])print ("Headless Chrome Initialized")params = {'behavior': 'allow', 'downloadPath': r'C:\Users\Debanjan.B\Downloads'}driver.execute_cdp_cmd('Page.setDownloadBehavior', params)driver.get("https://www.mockaroo.com/")driver.execute_script("scroll(0, 250)"); WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#download"))).click()print ("Download button clicked")#driver.quit()
  • Console Output:

    Headless Chrome InitializedDownload button clicked
  • File Downloading snapshot:

ChromeHeadlessDownload


Details

Downloading files through Headless Chromium was one of the most sought functionality since Headless Chrome was introduced.

Since then there were different work-arounds published by different contributors and some of them are:

Now the, the good news is Chromium team have officially announced the arrival of the functionality Downloading file through Headless Chromium.


In the discussion Headless mode doesn't save file downloads @eseckler mentioned:

Downloads in headless work a little differently. There's the Page.setDownloadBehavior devtools command to set a download folder. We're working on a way to use DevTools network interception to stream the downloaded file via DevTools as well.

A detailed discussion can be found at Issue 696481: Headless mode doesn't save file downloads

Finally, @bugdroid revision seems to have nailed the issue for us.


[ChromeDriver] Added support for headless mode to download files

Previously, Chromedriver running in headless mode would not properly download files due to the fact it sparsely parses the preference file given to it. Engineers from the headless chrome team recommended using DevTools's "Page.setDownloadBehavior" to fix this. This changelist implements this fix. Downloaded files default to the current directory and can be set using download_dir when instantiating a chromedriver instance. Also added tests to ensure proper download functionality.

Here is the revision and commit

From ChromeDriver v77.0.3865.40 (2019-08-20) release notes:

Resolved issue 2454: Headless mode doesn't save file downloads [Pri-2]

Solution


Outro

However Mac OSX users have a wait for their pie as On Chromedriver, headless chrome crashes after sending Page.setDownloadBehavior on MacOSX.


For javascript use below code:

    const chrome = require('selenium-webdriver/chrome');    let options = new chrome.Options();    options.addArguments('--headless --window-size=1500,1200');    options.setUserPreferences({ 'plugins.always_open_pdf_externally': true,    "profile.default_content_settings.popups": 0,    "download.default_directory": Download_File_Path });    driver = await new webdriver.Builder().setChromeOptions(options).forBrowser('chrome').build();

Then switch tabs as soon as you click the download button:

    await driver.sleep(1000);     var Handle = await driver.getAllWindowHandles();    await driver.switchTo().window(Handle[1]);


I don't think you should be using the browser for downloading content, leave it to Chrome developers/testers.

I believe you should rather get href attribute of the element you want to download and obtain it using requests library

If your site requires authentication you could fetch cookies from the browser instance and pass them to requests.Session.