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

Selenium Grid Docker环境下Chrome下载文件弹出保存对话框问题

Selenium Grid Chrome节点下载弹窗问题

问题描述

在Docker中搭建Selenium Grid,Hub基于selenium/hub:4.8.0镜像创建,节点基于selenium/node-chrome:4.8.0镜像创建。执行Python代码下载文件时,Google Chrome会弹出下载路径选择对话框,但此前使用selenium/standalone-chrome:3.141.59独立Selenium容器时,这段代码可正常运行。

会话截图

会话截图

执行代码

from selenium import webdriver
from selenium.common.exceptions import InvalidSessionIdException
from selenium.webdriver.common.by import By
from time import sleep

options = webdriver.ChromeOptions()
url = "http://localhost:4444/wd/hub"
options.add_argument("--window-size=1920,1080")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--disable-notifications")
prefs = {"profile.managed_default_content_settings.images": 2,
         "download.default_directory": "/home/seluser/download",
         "download.prompt_for_download": False,
         "download.directory_upgrade": True,
         "plugins.always_open_pdf_externally": True,
        }
options.add_experimental_option("prefs", prefs)
driver = webdriver.Remote(command_executor=url,
                          options=options)
driver.maximize_window()
driver.get("https://dornsife.usc.edu/assets/sites/298/docs/ir211wk12sample.xls")
sleep(5)

解决方法

  • 确保下载目录权限:启动节点容器时提前创建并授权下载目录,避免因权限不足触发弹窗。执行以下命令启动节点:
    docker run -d --link selenium-hub:hub selenium/node-chrome:4.8.0 sh -c "mkdir -p /home/seluser/download && chown seluser:seluser /home/seluser/download && /opt/bin/entry_point.sh"
    
  • 启用无头模式:在ChromeOptions中添加无头参数,避免可视化交互弹窗:
    options.add_argument("--headless=new")
    
  • 补充下载偏好设置:针对高版本Chrome,添加额外配置绕过安全校验和自动下载限制:
    prefs.update({
        "safebrowsing.enabled": True,
        "download.extensions_to_open": "",
        "profile.default_content_setting_values.automatic_downloads": 1
    })
    
  • 匹配版本兼容性:确保Python的selenium库版本与Grid版本一致,执行命令安装对应版本:
    pip install selenium==4.8.0
    

内容的提问来源于stack exchange,提问作者Yasin Arabi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 19:16:03