User Agents

Web browsers and everyday web tech, explained

Stop Selenium Chrome From Overwriting Downloads

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

When you automate downloads with Selenium and Chrome, the browser automatically appends "(1)" to a file name if a file with the same name already exists in the download folder. This happens because Chrome's default download behavior is to avoid overwriting. However, if you have set a custom download directory or are using a fresh profile, you might find that files are overwritten instead. This guide shows you how to configure Chrome options in Python to ensure that downloads are never overwritten—either by making Chrome always prompt for a location or by renaming files programmatically before saving.

What You'll Need

  • Python 3.6 or later installed.
  • Selenium library installed (pip install selenium).
  • Chrome browser installed (the version should match your ChromeDriver).
  • ChromeDriver downloaded and accessible in your PATH, or specify its location directly.
  • Basic knowledge of Python and Selenium WebDriver.

Step-by-Step

  1. Import required modules. Start by importing selenium.webdriver and selenium.webdriver.chrome.options.
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
  2. Create Chrome options object. Instantiate Options() to configure Chrome behavior.
    chrome_options = Options()
  3. Set download directory (optional but recommended). Use prefs to specify a custom download folder. This ensures you know exactly where downloads go.
    prefs = {"download.default_directory": "/path/to/your/downloads"}
  4. Disable the download prompt (if you want automatic saving). By default, Chrome does not prompt for downloads unless you set download.prompt_for_download to True. To avoid overwriting, you can set it to True so Chrome asks where to save each file. However, this can interrupt automation. A better approach is to keep it False and handle renaming manually.
  5. Prevent overwriting by using unique file names. The most reliable method is to check if a file with the target name exists in the download directory before the download starts, and if so, rename the existing file or choose a new name for the download. You can do this by monitoring the download folder before and after the download.
  6. Implement a wait-for-download function. After clicking a download link, wait until the file appears in the download directory. Use time.sleep() or better, WebDriverWait with a condition that checks for file existence.
  7. Rename existing files before downloading. Before triggering the download, check if the target file name exists. If it does, rename it (e.g., append a timestamp) so the new download can use the original name without conflict.
    import os
    if os.path.exists(download_path):
    os.rename(download_path, download_path + ".old")
  8. Set Chrome preferences to avoid overwriting. If you want Chrome to automatically append numbers (like "file (1).txt") instead of overwriting, you can set the preference download.prompt_for_download to False and download.extensions_to_open to an empty list. But note: Chrome's default behavior already avoids overwriting by appending numbers. If you are seeing overwrites, it's likely because you are using a custom profile that has a different setting, or you are using a headless browser with a fresh profile that might behave differently.
  9. Add the preferences to Chrome options.
    chrome_options.add_experimental_option("prefs", prefs)
  10. Initialize the driver. Pass the options to webdriver.Chrome().
    driver = webdriver.Chrome(options=chrome_options)
  11. Test your setup. Navigate to a page with a download link, click it, and verify that the file is saved without overwriting an existing file. If you used the renaming method, check that the old file was renamed and the new file has the original name.

Common Mistakes

  • Forgetting to set the download directory. If you don't specify a directory, Chrome uses the default (e.g., ~/Downloads). If you check for files in a different folder, you'll never find them.
  • Using download.prompt_for_download incorrectly. Setting it to True will cause a dialog to appear, which Selenium cannot easily interact with. This can hang your script.
  • Not waiting for the download to complete. If you check for the file immediately after clicking, it may not exist yet. Always wait until the file is fully written.
  • Assuming Chrome always appends numbers. Some versions or profiles may not. Always test explicitly.
  • Renaming the wrong file. If you rename before the download, you might rename a partially downloaded file from a previous attempt.

Tips

  • Use a unique download folder per test run. Create a temporary directory with tempfile.mkdtemp() and set it as the download directory. This completely avoids conflicts.
  • Use os.listdir() to monitor the folder. Before and after the download, list files and compare to find the new one.
  • Consider using time.sleep() with a loop. Poll for file existence with a timeout to avoid indefinite waiting.
  • If you need to download many files, use a naming scheme with timestamps. For example, file_20250321_1530.pdf.
  • For headless Chrome, the same options work. Just add --headless to the arguments, but note that some file types may not download in headless mode without additional flags.

Troubleshooting

  • Downloads still overwrite. Check if you are using a user-data-dir that has a custom preference. Remove the --user-data-dir argument or create a fresh profile.
  • Chrome prompts for download location. If you see a dialog, it means download.prompt_for_download is set to True. Set it to False in prefs.
  • File names have "(1)" appended. That's Chrome's default behavior to avoid overwriting. If you want to prevent that, you need to rename the existing file before downloading, as described in Step 7.
  • Download doesn't start. Ensure your click action actually triggers the download. Sometimes the link opens a new tab or requires a right-click. Use driver.get() directly if it's a direct link.
  • Headless mode doesn't download. Add --disable-gpu and --no-sandbox if running as root, and ensure the download directory is writable.

Frequently Asked Questions

Does Chrome overwrite files by default?

No. Chrome's default behavior is to append a number in parentheses (e.g., "file (1).pdf") to avoid overwriting. If you see overwriting, it's usually because you've set a custom preference or are using a profile that changed this.

How do I force Chrome to always ask where to save?

Set download.prompt_for_download to True in preferences. However, this will create a dialog that Selenium cannot easily handle, so it's not recommended for automation.

Can I disable the number suffix in Chrome?

There is no official preference to disable the suffix. Instead, you should rename existing files before downloading or use a unique directory each time.

Why does my download folder contain both the old and new file?

If you rename the old file before downloading, you'll have both. That's expected. If you want to replace, delete the old file instead of renaming.

How do I wait for a download to complete in Selenium?

Poll the download directory for the file's existence and check that its size stops changing. Use a loop with time.sleep() and a timeout.

Does this work with ChromeDriver and Selenium 4?

Yes, the options and preferences are the same. Just ensure you have the latest ChromeDriver and Selenium version.