You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python+Selenium处理Chrome浏览器下载对话框

Hey there! I totally get how frustrating it is to dig up tons of Firefox-focused solutions when you’re stuck on Chrome-specific download issues. Let’s cover both approaches you’re looking for to get this sorted.

方案1:控制Chrome下载弹窗(自定义文件名+点击保存)

First off, Chrome’s download popup is a native system control—not part of the webpage DOM—so Selenium alone can’t directly interact with it. But you can use desktop automation tools to mimic user actions. Here’s a Python example using PyAutoGUI:

前提准备

Install PyAutoGUI first:

pip install pyautogui

示例代码

import pyautogui
import time
from selenium import webdriver

# Initialize ChromeDriver
driver = webdriver.Chrome()
driver.get("your-target-page-url")

# Trigger the download action (adjust selector to match your download button)
download_btn = driver.find_element("css selector", "button.download-button")
download_btn.click()

# Wait for the download popup to load (tweak the sleep time based on your internet speed)
time.sleep(2)

# If the filename input isn't focused by default, click its position first
# Use pyautogui.position() to get the exact coordinates of the input box
# pyautogui.click(x=450, y=580)

# Type your custom filename
pyautogui.typewrite("my-custom-image.png")

# Press Enter to save, or click the save button via coordinates
pyautogui.press("enter")
# pyautogui.click(x=680, y=580)

# Clean up
driver.quit()

Note: This method relies on screen coordinates, so you’ll need to adjust them to match your screen resolution and Chrome’s popup placement. It’s a bit fragile compared to the second approach, but it works if you absolutely need to interact with the popup.

方案2:配置ChromeDriver自动忽略弹窗并自动保存

This is the more reliable approach—we’ll configure Chrome to skip the download prompt entirely and save files straight to a specified folder. Here’s how to do it in Python:

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

# Set your desired download directory (replace with your own path)
download_directory = "C:/Users/YourUsername/Downloads/ChromeAutoSaves"

# Create the directory if it doesn't exist
if not os.path.exists(download_directory):
    os.makedirs(download_directory)

# Configure Chrome preferences
chrome_prefs = {
    "download.default_directory": download_directory,
    "download.prompt_for_download": False,  # Disable the download popup
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True,
    # For image files, Chrome usually saves them automatically without extra prompts
}

chrome_options = Options()
chrome_options.add_experimental_option("prefs", chrome_prefs)

# Initialize ChromeDriver with the configured options
driver = webdriver.Chrome(options=chrome_options)
driver.get("your-target-page-url")

# Trigger the download
download_btn = driver.find_element("css selector", "button.download-button")
download_btn.click()

# Wait for the download to complete (adjust based on file size)
time.sleep(4)

# Optional: Rename the downloaded file to your custom name
original_file_path = os.path.join(download_directory, "original-image-name.png")
custom_file_path = os.path.join(download_directory, "my-custom-image.png")
if os.path.exists(original_file_path):
    os.rename(original_file_path, custom_file_path)

# Clean up
driver.quit()

This method avoids any interaction with native popups entirely, making it much more stable across different environments.

内容的提问来源于stack exchange,提问作者Ian Spitz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:28:08