Uploading Multiple Nested Folders with Python & Selenium in Headless Chrome Mode

What will you learn?

In this tutorial, you will learn how to automate the process of uploading a folder containing multiple subfolders using Python and Selenium WebDriver in headless Chrome mode. This automation skill is valuable for tasks like automating file uploads on websites, especially when dealing with directories with nested folders.

Introduction to the Problem and Solution

Uploading files and folders manually can be tedious, particularly when handling directories with nested subfolders. By utilizing Python along with Selenium WebDriver, we can automate this process efficiently, even in headless Chrome mode. Headless Chrome operates without a graphical user interface, making automation scripts faster and suitable for environments without displays.

The solution involves configuring Selenium WebDriver for headless operation, navigating to the target webpage for upload, and programmatically interacting with file input elements to select the directory for upload. Understanding how web browsers handle file uploads and replicating these actions through code is essential.


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

def upload_folder(path_to_chromedriver, url_of_upload_page, path_to_folder):
    chrome_options = Options()

    driver = webdriver.Chrome(executable_path=path_to_chromedriver,

        folder_input = driver.find_element(By.ID, "your-folder-input-element-id")

        print("Folder uploaded successfully!")


path_to_chromedriver = "/path/to/chromedriver"
url_of_upload_page = "http://example.com/upload"
path_to_folder = "/path/to/your/folder"
upload_folder(path_to_chromedriver, url_of_upload_page, path_to_folder)

# Copyright PHD


The provided Python script showcases how to use Selenium WebDriver with headless Chrome to upload an entire directory structure to a website. The key steps include: – Setting up the webdriver instance for headless mode. – Navigating to the target webpage. – Locating the directory input element on the webpage. – Simulating file selection by sending keys (absolute path) to the input field.

Ensure that both chromedriver executable path and directory path are absolute.

    1. Can I use Firefox instead of Chrome? Yes! You can switch to Geckodriver and configure it for Firefox.

    2. What happens if my internet connection is lost during execution? Selenium operations might timeout based on specified conditions.

    3. Is it possible to monitor progress while running in headless mode? While visual feedback isn’t available, logging mechanisms or screenshots can provide insights into execution progress.

    4. Can I interact with dialog boxes using this method? Indirectly; workarounds like pyautogui may be needed for native OS dialogs.

    5. Will this method work across all operating systems? Primarily yes but slight modifications may be required based on platform nuances.


Automating file uploads using Python and Selenium offers significant efficiency gains over manual methods. This approach allows customization tailored to specific needs while addressing challenges effectively throughout development cycles. By leveraging tools like Python and Selenium, you can streamline tasks efficiently while ensuring smooth operational experiences consistently over time.

Leave a Comment