5 Best Ways to Automatically Download Files From a Pop-Up Dialog Using Selenium Python

Rate this post

πŸ’‘ Problem Formulation: Developers often encounter scenarios where they need to automate the process of downloading files from a web application. This challenge may involve handling pop-up dialogs, which browsers typically use for file downloads. The input in this case is a web page with a pop-up dialog triggering a file download, and the desired output is the automatic retrieval of the file without manual intervention using Selenium Python.

Method 1: Configure Browser Preferences

One common approach is to configure browser preferences to disable the pop-up dialog when a download is initiated. For instance, specifying preferences in Firefox using Selenium allows the browser to download files to a predefined directory without asking for a confirmation. This method is advantageous as it simplifies the download process and is particularly useful for batch downloads.

Here’s an example:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.manager.showWhenStarting", False)
options.set_preference("browser.download.dir", "/path/to/dir")
options.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/pdf")

driver = webdriver.Firefox(options=options)

Output: PDF file downloaded to the specified directory without a pop-up dialog.

This code snippet initializes a Firefox browser instance with specified preferences that direct the browser to automatically download files of a PDF type to a designated directory. It bypasses the download prompt, making the download process seamless.

Method 2: Using Chrome WebDriver

Google Chrome also allows you to manipulate its settings for file downloads using Chrome WebDriver. By customizing Chrome’s download behavior through desired capabilities, you can instruct Chrome to download files automatically to a specified path without interaction. This is particularly useful when working with a headless browser where GUI interactions are not possible.

Here’s an example:

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

chrome_options = Options()
chrome_options.add_experimental_option("prefs", {
    "download.default_directory": "/path/to/dir",
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True

driver = webdriver.Chrome(options=chrome_options)

Output: The desired file is downloaded to the pre-set directory.

In the given code snippet, we configure a Chrome driver with options that set download behaviors. It involves setting the download path, disabling download prompts, and ensuring the download directory gets upgraded if necessary.

Method 3: Handling Pop-Ups with Robot Class (Java)

For situations where browser preferences cannot be set, and there’s a need to interact with OS-level pop-ups, the Java Robot class can be utilized in combination with Selenium. Even though this method is not directly related to Python, it’s worth mentioning for those who can use Java. It emulates keyboard and mouse actions to interact with the pop-up and automate the download.

This method is not demonstrated here as it falls out of Python scope but it is similar to configuring a Robot object in Java that sends keystroke and mouse event to the download dialog.

Method 4: Utilize Selenium WebDriver Wait Conditions

Explicit and implicit waits can be used in Selenium to handle scenarios where the download dialog takes some time to appear. By using WebDriverWait, we can set the conditions that must be satisfied before proceeding, ensuring that our script only tries to download the file once the dialog is ready.

Here’s an example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
download_link = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "download-link"))

# Assuming here the file is downloaded without further pop-ups due to browser setup.

Output: File download is initiated after the download link is visible and clickable.

In this script, we navigate to the download page and wait for up to 10 seconds for the download link to become available. Once it is available, we simulate a click on the link, which starts the download process.

Bonus One-Liner Method 5: Using WebDriver’s execute_script

In certain circumstances, it may be possible to bypass the download dialog entirely by executing a direct download script via the browser’s console, using Selenium’s execute_script method. This technique is highly dependent on the specific web application and the availability of a direct download command or API.

Here’s an example:


Output: File is downloaded directly without interacting with the pop-up.

The shown code invokes a JavaScript function directly that is assumed to be responsible for the file download process. This method is very specific and would only work if such a function is defined and exposed within the web application you are automating.


  • Method 1: Configuring Browser Preferences. Strengths: Straightforward, suitable for batch downloads. Weaknesses: Limited to supported preferences and file types.
  • Method 2: Using Chrome WebDriver. Strengths: Similar to Method 1, tailored for Chrome. Weaknesses: Specific to Chrome, requires ChromeDriver compatibility.
  • Method 3: Handling Pop-Ups with Robot Class. Strengths: Powerful in handling native dialogs. Weaknesses: Platform-specific, only applicable to Java.
  • Method 4: Selenium WebDriver Wait Conditions. Strengths: Ensures elements are ready before interaction. Weaknesses: Requires accurate wait conditions, still relies on browser configuration for downloads.
  • Bonus One-Liner Method 5: Using WebDriver’s execute_script. Strengths: Can bypass dialogs entirely. Weaknesses: Highly web application-specific, requires existing download function in the web app.