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

Azure DevOps运行Selenium自动化时出现WebDriverError: BROWSER_TIMEOUT问题求助

解决WebDriverError: Session was terminated due to BROWSER_TIMEOUT(Azure DevOps环境)

针对你在Azure DevOps执行Selenium自动化测试时遇到的浏览器超时问题(本地环境正常),可以从以下几个方向排查修复:

1. 显式配置Selenium与浏览器超时参数

本地环境资源充足、网络稳定,而CI环境可能默认超时阈值较短。在测试代码中手动设置各类超时:

  • 设置页面加载、隐式等待时长:
    # Python示例,其他语言逻辑一致
    from selenium import webdriver
    
    driver = webdriver.Chrome()
    # 页面加载超时设为60秒
    driver.set_page_load_timeout(60)
    # 隐式等待元素出现的超时设为10秒
    driver.implicitly_wait(10)
    
  • 给浏览器添加启动参数延长自身超时:
    options = webdriver.ChromeOptions()
    # 页面加载超时(毫秒)
    options.add_argument("--timeout=60000")
    # 禁用可能导致超时的网络服务特性(按需使用)
    options.add_argument("--disable-features=NetworkService")
    driver = webdriver.Chrome(options=options)
    

2. 优化Azure DevOps代理资源配置

Azure托管代理默认资源规格较低,容易因浏览器资源不足触发超时:

  • 切换到更高规格的托管代理:比如选择Windows 2022(4核/16GB)或Linux(4核/16GB)规格的代理,替代默认的2核/7GB配置
  • 启用浏览器无头模式减少资源占用:
    options.add_argument("--headless=new")
    options.add_argument("--disable-gpu")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")  # 解决Linux代理内存不足问题
    
  • 自托管代理需关闭后台无关进程,确保CPU、内存充足

3. 调整Azure DevOps任务超时设置

平台默认的任务超时可能提前终止测试进程:

  • 在管道任务中延长超时时间:比如将测试脚本任务的timeoutInMinutes设为60(默认30)
  • YAML管道示例:
    - task: PythonScript@0
      inputs:
        scriptSource: 'filePath'
        scriptPath: 'tests/run_selenium_tests.py'
      timeoutInMinutes: 60
    

4. 解决网络延迟问题

代理与应用服务器跨区域会导致加载缓慢:

  • 选择与Web应用同区域的Azure托管代理
  • 添加网络请求重试机制,应对临时延迟:
    from selenium.common.exceptions import WebDriverException
    import time
    
    def load_url_with_retry(driver, url, max_retries=3):
        for attempt in range(max_retries):
            try:
                driver.get(url)
                return
            except WebDriverException as e:
                if "BROWSER_TIMEOUT" in str(e) and attempt < max_retries -1:
                    time.sleep(5)
                    continue
                raise
    

5. 确保依赖版本兼容

CI环境的Selenium、浏览器、驱动版本可能与本地不匹配:

  • 使用webdriver-manager自动管理兼容的驱动版本:
    from selenium.webdriver.chrome.service import Service as ChromeService
    from webdriver_manager.chrome import ChromeDriverManager
    
    driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
    
  • 在CI脚本中显式安装指定版本的Selenium:
    pip install selenium==4.15.0
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 15:52:44