You need to enable JavaScript to run this app. Headless Edge Selenium报No browser is open错误解决求助
优惠活动
大模型
产品
解决方案
定价
更多

Headless Edge Selenium报No browser is open错误解决求助

Selenium启动Edge无头模式报错“No browser is open”解决方案

核心原因

该错误由三个常见配置问题共同导致:

  1. 依赖包过时:msedge.selenium_tools是微软早期适配Chromium Edge的过渡包,目前已完全弃用,对新版Edge无头模式兼容性极差
  2. 驱动不匹配:使用的MicrosoftWebDriver.exe是旧版Edge Legacy(非Chromium内核)的驱动,和当前Chromium内核的Edge不兼容
  3. 启动参数格式错误:无头模式相关参数缺少前缀--,无法被Edge正确识别

解决步骤

  • 第一步:清理旧依赖,安装最新版Selenium
    执行以下命令:
    pip uninstall msedge-selenium-tools selenium -y
    pip install selenium
    
  • 第二步:驱动适配(可省略)
    Selenium4支持自动匹配与本地Edge版本对应的msedgedriver驱动,无需手动下载配置。如果需要手动指定驱动,需下载和本地Edge版本完全一致的msedgedriver.exe替换原有的MicrosoftWebDriver.exe。
  • 第三步:修正启动代码
    正确配置的代码示例如下:
    from selenium import webdriver
    from selenium.webdriver.edge.options import Options
    
    def function():
        edge_options = Options()
        # 新版无头模式,兼容性远高于旧版headless
        edge_options.add_argument('--headless=new')
        edge_options.add_argument('--disable-gpu')
        # 额外添加兼容参数,避免特殊环境下启动异常
        edge_options.add_argument('--no-sandbox')
        edge_options.add_argument('--disable-dev-shm-usage')
        
        # 无需手动指定驱动路径,Selenium会自动匹配
        driver = webdriver.Edge(options=edge_options)
        # 测试启动正常性
        driver.get("https://www.baidu.com")
        print(driver.title)
        driver.quit()
    

兼容方案(不更换旧依赖)

如果暂时需要继续使用msedge.selenium_tools,只需修改两处配置即可修复问题:

  1. 给启动参数添加--前缀:edge_options.add_argument('--headless')、edge_options.add_argument('--disable-gpu')
  2. 将原驱动替换为对应版本的msedgedriver.exe

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 00:54:03