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

Python Selenium操控Chrome时如何避免下载路径选择弹窗?

解决Chrome + Selenium自动下载弹窗问题的实用方案

1. 补加高版本Chrome适配配置项

Chrome 115+对下载权限逻辑做了调整,原来的配置不够用了,得加上几个新参数:

chrome_options.add_experimental_option('prefs', {
    'download.default_directory': path_download,
    'download.directory_upgrade': True,
    'download.prompt_for_download': False,
    'profile.default_content_setting_values.notifications': 2,
    'profile.default_content_setting_values.automatic_downloads': 1,
    # 新增适配高版本的关键配置
    'safebrowsing.enabled': True,
    'download_restrictions': 0,
    'profile.default_content_settings.popups': 0
})

2. 确认下载路径是合法绝对路径

  • 必须用绝对路径,相对路径会让Chrome找不到位置
  • Windows路径别忘转义,要么用双反斜杠,要么加r前缀:
    path_download = r"C:\Users\YourName\Downloads\Selenium_Downloads"
    
  • 先手动创建好这个文件夹,Chrome不会自动生成不存在的目录

3. 关闭Chrome的下载保护机制

高版本Chrome的安全拦截会打断自动下载,加上这两个启动参数:

chrome_options.add_argument("--safebrowsing-disable-download-protection")
chrome_options.add_argument("--safebrowsing-disable-extension-blacklist")

4. 确保WebDriver和Chrome版本严格匹配

虽然webdriver-manager会自动匹配,但手动指定版本更稳妥:

from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service

# 强制安装和当前Chrome完全一致的Driver版本
service = Service(ChromeDriverManager(version="117.0.5938.92").install())
driver = webdriver.Chrome(service=service, options=chrome_options)

5. 清理浏览器缓存和旧配置

旧的用户配置可能冲突,每次运行用临时目录或者无痕模式:

# 用临时配置目录
chrome_options.add_argument("--user-data-dir=C:\\Temp\\ChromeProfile")
# 禁用扩展避免干扰
chrome_options.add_argument("--disable-extensions")
# 可选:用无痕模式彻底规避缓存
chrome_options.add_argument("--incognito")

内容的提问来源于stack exchange,提问作者Douglas Gonçalves

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 10:22:16