使用Python Selenium修改Edge浏览器默认下载目录的方法
Selenium 配置Edge浏览器自定义下载目录实现
Edge(Chromium内核版本)的配置逻辑和Chrome基本一致,核心差异是需要使用Edge专属的配置类,以下是可直接运行的代码:
import os from selenium.webdriver.edge.options import Options as EdgeOptions from selenium.webdriver.edge.service import Service from selenium import webdriver # 自定义下载路径,和你之前Chrome的路径写法完全兼容 DownloadPath = f'C:\\Users\\{EmplyID}\\Desktop\\General Orders\\{Newfolder}' # 提前创建目标目录,路径不存在时Edge会自动回退到系统默认下载路径 os.makedirs(DownloadPath, exist_ok=True) edge_options = EdgeOptions() # 下载相关配置项和Chrome的prefs字段完全通用 download_prefs = { "download.default_directory": DownloadPath, # 以下为可选配置,用于关闭下载确认弹窗、开启安全校验,避免自动化流程被阻塞 "download.prompt_for_download": False, "download.directory_upgrade": True, "safebrowsing.enabled": True } edge_options.add_experimental_option("prefs", download_prefs) # 加载Edge驱动,注意要使用和本地Edge版本匹配的msedgedriver,不能复用chromedriver edge_driver_path = f'C:\\Users\\{EmplyID}\\Desktop\\General Orders\\Bidman To Enovia\\msedgedriver.exe' # Selenium 4+ 推荐用Service类加载驱动,兼容新旧版本 service = Service(executable_path=edge_driver_path) driver = webdriver.Edge(service=service, options=edge_options)
常见配置不生效的排查点
- 确认使用的是和本地Edge浏览器大版本号完全对应的
msedgedriver.exe,版本不匹配不仅会启动失败,也可能导致配置项不生效 - 如果你用的是3.x版本的Selenium,可以去掉Service相关写法,直接在初始化
webdriver.Edge时传入executable_path=edge_driver_path参数,和你之前写Chrome的代码格式完全一致 - 不要给Edge配置额外的无关启动参数,部分参数会覆盖prefs里的下载路径设置
- 确保运行脚本的用户对自定义下载目录有写入权限,权限不足时Edge也会回退到默认下载目录
内容的提问来源于stack exchange,提问作者Vishal
相关产品推荐
相关产品推荐

