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

受保护服务器上启动msedgedriver.exe后Edge无法启动(C#/Selenium)

Selenium EdgeDriver在Windows Server 2019代理环境下启动失败

我有一套测试自动化框架,本地运行正常,通过DriverManager获取对应版本的msedgedriver.exe后启动Edge执行测试脚本。

部署到客户的Windows Server 2019服务器(处于代理环境,用户拥有本地管理员权限)后,用VS 2022运行测试时,msedgedriver.exe已在任务管理器中启动,但测试失败,报错如下:

Message: 
OpenQA.Selenium.WebDriverException : Cannot start the driver service on http://localhost:53325/

Stack Trace: 
DriverService.Start()
DriverServiceCommandExecutor.Execute(Command commandToExecute)
WebDriver.Execute(String driverCommandToExecute, Dictionary2 parameters) 
WebDriver.StartSession(ICapabilities desiredCapabilities) 
WebDriver.ctor(ICommandExecutor executor, ICapabilities capabilities) 
ChromiumDriver.ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout) 
EdgeDriver.ctor(EdgeDriverService service, EdgeOptions options, TimeSpan commandTimeout) 
EdgeDriver.ctor(EdgeDriverService service, EdgeOptions options) BrowserFeature.OpenBrowser() line 109

通过Edge浏览器可访问http://localhost:53325/(或其他启动端口),但Edge浏览器本身从未启动。

启动代码如下:

var edgeOptions = new EdgeOptions();
edgeOptions.Proxy = proxy;
edgeOptions.SetLoggingPreference(LogType.Driver, LogLevel.All);
edgeOptions.AddArguments("--no-sandbox");

var service = EdgeDriverService.CreateDefaultService();
service.UseVerboseLogging = true;
edgeOptions.AddUserProfilePreference("download.default_directory", Settings.DownloadFolder);
                   
Driver = new EdgeDriver(service, edgeOptions);

已尝试的排查手段:

  • 以管理员模式运行VS 2022
  • 在系统变量中添加HTTP_PROXY、HTTPS_PROXY和NO_PROXY,前两者填写代理服务器,NO_PROXY包含localhost和127.0.0.1
  • 检查msedgedriver是否重复运行,确认无此情况
  • 在代码(含DriverManager)中配置代理,因msedgedriver已正确下载,该操作未解决问题
  • 弃用DriverManager,直接集成并启动对应版本的驱动
  • 将Edge替换为Chrome执行上述操作,出现完全相同的错误
  • 确认驱动版本与浏览器版本匹配
  • 关闭Windows防火墙
  • 在注册表中添加DeveloperToolsAvailability变量并设值为"1",启用Edge开发者工具

目前仍无头绪,相同代码在本地及另一台服务器上均可正常运行,推测客户服务器存在阻止浏览器启动或驱动通信的配置。

进一步排查发现,OpenQA.Selenium.DriverService中用于检测驱动是否启动成功的IsInitialized代码如下:

protected virtual bool IsInitialized
{
    get
    {
        bool result = false;
        try
        {
            HttpClient httpClient = new HttpClient();
            try
            {
                httpClient.DefaultRequestHeaders.ConnectionClose = true;
                httpClient.Timeout = TimeSpan.FromSeconds(5.0);
                Uri serviceHealthUri = new Uri(ServiceUrl, new Uri(DriverCommand.Status, UriKind.Relative));
                using HttpResponseMessage httpResponseMessage = Task.Run(async () => await httpClient.GetAsync(serviceHealthUri)).GetAwaiter().GetResult();
                result = httpResponseMessage.StatusCode == HttpStatusCode.OK && httpResponseMessage.Content.Headers.ContentType!.MediaType!.StartsWith("application/json", StringComparison.OrdinalIgnoreCase);
                return result;
            }
            finally
            {
                if (httpClient != null)
                {
                    ((IDisposable)httpClient).Dispose();
                }
            }
        }
        catch (Exception ex) when (ex is HttpRequestException || ex is TaskCanceledException)
        {
            Console.WriteLine(ex.Message);
            return result;
        }
    }
}

随后固定端口号,编写PowerShell脚本模拟上述检测逻辑,在测试报错(OpenQA.Selenium.WebDriverException : Cannot start the driver service on http://localhost:5588/)前,脚本显示服务正常运行:

PowerShell脚本:

# Set the URL for the WebDriver service status check
$serviceUrl = "http://localhost:5588/status"

# Attempt to make an HTTP GET request to the service URL
try {
    $response = Invoke-WebRequest -Uri $serviceUrl -TimeoutSec 5

    # Check if the response is successful (status code 200)
    if ($response.StatusCode -eq 200) {
        # Check the content type (case-insensitive)
        $contentType = $response.Headers["Content-Type"]
        if ($contentType -eq "application/json" -or $contentType -ilike "application/json*") {
            Write-Host "WebDriver service is running and responsive."
        } else {
            Write-Host "Unexpected content type: $contentType"
        }
    } else {
        Write-Host "Error: $($response.StatusCode)"
    }
} catch {
    Write-Host "Error: $_"
}

echo $response.StatusCode

脚本响应:

WebDriver service is running and responsive.
200

显然代码中的检测逻辑无法访问http://localhost:5588/,求解决办法。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 00:03:12