I have seen a lot of conversations are going on StackOverflow and other platforms on – how to download a File in headless mode using python in selenium, this making me todo this article, let’s see.

Selenium Download a File in Headless Mode:

In this tutorial, I am going to give a concrete example on Selenium Download a File in Headless Mode.

This example requires the following Softwares to be installed in your machine.

You can follow my previous article about how to install the selenium and kickstart with selenium HelloWorld example to get all prerequisites.

Versions:

I am running this example on the below software versions.

  • Python3.7
  • pip 20.1
  • Selenium 3.141
  • Chrome Browser  81.0.4044.138
  • Chrome Driver 83.0.4103.39

What is Headless Mode?

Headless is a way to run the browser in a headless environment without the full browser UI. It allows you to run the browser in on a server environment (usually servers do not have UI). Generally, Headless browser gives us a real browser context without the memory overhead of running a full version of Chrome.

Download a File in Headless Mode:

As part of this example, I am going to download a python_samples-master.zip file from my GIT repository in headless mode.

selenium_filedownloader.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
import time

DOWNLOAD_URL = "https://github.com/chandrashekhargoka/python_samples/archive/master.zip"
download_dir = "D:\\softwares\\filedownloads\\"
driver_path = "D:\\softwares\\chromedriver_win\\chromedriver.exe"

def enable_download(driver):
    driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
    params = {'cmd':'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': download_dir}}
    driver.execute("send_command", params)

def setting_chrome_options():
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument('--no-sandbox')
    return chrome_options;

def isFileDownloaded():
    file_path = download_dir+"\\python_samples-master.zip"
    while not os.path.exists(file_path):
        time.sleep(1)
    if os.path.isfile(file_path):
        print("File Downloaded successfully..")

if __name__ == '__main__':
    driver = webdriver.Chrome(executable_path=driver_path,options=setting_chrome_options())
    enable_download(driver)
    driver.get(DOWNLOAD_URL)
    isFileDownloaded()

Code Walkthrough:

  • enable_download() function is responsible to set the driver parameters with download path and executes the driver with POST request. It allows the download process as a POST call.
  • We enabling the headless mode for chrome driver using the Options object. settings_chrome_options() function creates Options object and enables the --headless, --no-sandbox modes and returns the options object. This has to be set to Chrome driver while creating it.
  • isFileDownloaded() function keep on checking the download path for every 1 second, whether the file has been downloaded or not. If the file downloaded on the given path, it stops checking and prints File Downloaded Successfully message.

Output:

Run the above program – This time you won’t see the browser window but if everything went well you could see the below output in the console.

File Downloaded successfully..

And you could also see the downloaded file in the given path.

Python - Selenium Download a File in Headless Mode

Happy Learning 🙂