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

Azure Pipelines中ChromeDriver误识别为Linux环境导致路径错误问题

问题根因

从日志可以直接定位核心问题:你当前使用的Azure Pipeline执行代理为Linux环境,和你本地的Windows环境不一致。

  • 日志里的chromedriver缓存路径/home/vsts/.wdm/drivers/chromedriver/linux64/...是标准Linux系统的用户目录格式,不是WDM识别错误,是工具自动检测到运行环境为Linux x64,所以拉取了对应平台的驱动版本,和你本地Windows环境拉取win32版本驱动的逻辑完全一致。
  • 你代码中硬编码的Chrome安装路径、chromedriver路径全是Windows专属的C盘绝对路径,在Linux代理上不存在对应文件,必然触发找不到chrome binary的报错。
  • 额外说明:你写的options.chrome_driver_binary是Selenium旧版本的废弃参数,在新版Selenium中不会生效,驱动路径需要通过Service对象传入,写在options配置里没有任何作用。
修复方案

根据你实际需要的运行环境二选一即可,不需要硬编码跨环境不兼容的绝对路径。

方案1:使用Windows代理运行(和本地环境完全一致)

直接修改Azure Pipeline的代理池配置,选择windows-latest规格的微软托管代理,替换当前使用的ubuntu-latestLinux代理即可。
同时把代码中硬编码的路径全部删除,改成自动适配环境的写法,避免换环境就失效,参考代码如下:

import pytest
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

@pytest.fixture
def get_chrome_options():
    options = ChromeOptions()
    options.add_argument("--start-maximized")
    options.add_argument("--no-sandbox")
    options.add_argument("--disable-dev-shm-usage")
    # CI环境无桌面,必须开启无头模式
    options.add_argument("--headless=new")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("useAutomationExtension", False)
    return options

@pytest.fixture
def get_webdriver(get_chrome_options):
    options = get_chrome_options
    # WDM会自动匹配当前系统、当前Chrome版本的对应驱动,无需手动指定路径
    driver = webdriver.Chrome(options=options, service=Service(ChromeDriverManager().install()))
    yield driver
    driver.quit()

微软托管的Windows代理默认预装了Chrome稳定版,Selenium和WDM会自动识别Chrome安装路径,不需要手动配置binary_location。

方案2:沿用现有Linux代理运行

如果不需要和本地Windows环境对齐,就用当前的Linux代理跑测试,直接用上面给出的跨环境适配代码即可,删掉所有Windows专属的硬编码路径。
Linux环境下Chrome默认安装到系统路径,WDM会自动拉取对应Linux版本的chromedriver,不需要手动指定路径。如果代理上没有预装Chrome,在Pipeline的前置执行步骤中通过系统包管理器安装Google Chrome稳定版即可。

注意事项
  • 不要在自动化测试代码中硬编码本地机器的绝对路径,这类配置换运行环境就会失效,路径匹配逻辑交给WebDriver Manager自动处理即可。
  • 所有CI/CD环境都是无桌面的服务器环境,跑Selenium必须加无头模式启动参数,否则会触发浏览器启动失败的错误。
  • 测试用例执行完成后要主动调用driver.quit()释放资源,避免Pipeline跑完后残留进程导致任务异常挂起。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 00:03:20