Jenkins中Selenium+C#自动化测试Edge浏览器启动崩溃问题求助
Edge浏览器Selenium自动化启动崩溃问题排查
问题背景
本地Jenkins流水线中,Selenium结合C#编写的自动化测试用Chrome浏览器运行正常,但切换为Edge浏览器时出现启动崩溃错误,报错提示DevToolsActivePort文件不存在,msedgedriver判定Edge已崩溃。
第一段初始化代码
var options = new EdgeOptions(); options.UseChromium = true; options.AddArgument("disable-gpu"); options.AddUserProfilePreference("download.default_directory", directoryPath); options.AddUserProfilePreference("intl.accept_languages", "en"); options.AddUserProfilePreference("disable-popup-blocking", "true"); options.AddUserProfilePreference("download.prompt_for_download", false); options.AddUserProfilePreference("download.directory_upgrade", true); options.AddUserProfilePreference("credentials_enable_service", false); options.AddUserProfilePreference("plugins.always_open_pdf_externally", true); options.AddUserProfilePreference("profile.password_manager_enabled", false); options.AddUserProfilePreference("pdfjs.disabled", true); options.AddArguments("--disable-backgrounding-occluded-windows"); options.AddArgument("--no-sandbox"); options.AddArgument("start-maximized"); var edgeInfo = FileVersionInfo.GetVersionInfo(ConfigurationManager.AppSettings["EdgePath"]); var version = edgeInfo.FileVersion; TestContext.Progress.WriteLine($"Edge executable path (under EdgePath in CommonSettings.config): {ConfigurationManager.AppSettings["EdgePath"]}. Edge version: {version}"); new DriverManager().SetUpDriver(new EdgeConfig(), version); Driver = new EdgeDriver(options); break;
报错信息
OpenQA.Selenium.WebDriverException : 未知错误:Microsoft Edge启动失败:已崩溃。 (未知错误:DevToolsActivePort文件不存在) (从msedge路径C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe启动的进程已不再运行,因此msedgedriver判定msedge已崩溃。) TearDown : System.NullReferenceException : 对象引用未设置为对象的实例。 堆栈跟踪: 在 OpenQA.Selenium.WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute) 在 OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) 在 OpenQA.Selenium.WebDriver.StartSession(ICapabilities desiredCapabilities) 在 OpenQA.Selenium.WebDriver..ctor(ICommandExecutor executor, ICapabilities capabilities) 在 OpenQA.Selenium.Chromium.ChromiumDriver..ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout) 在 OpenQA.Selenium.Edge.EdgeDriver..ctor(EdgeDriverService service, EdgeOptions options, TimeSpan commandTimeout) 在 OpenQA.Selenium.Edge.EdgeDriver..ctor(String edgeDriverDirectory, EdgeOptions options, TimeSpan commandTimeout) 在 OpenQA.Selenium.Edge.EdgeDriver..ctor(String edgeDriverDirectory, EdgeOptions options) 在 Moshocart.Tests.Infrastructure.TestCore.Launch(String browserType) 位置 C:\ProgramData\Jenkins\.jenkins\workspace\UIDemo\Tests\Infrastructure\TestCore.cs:行号 179 在 Moshocart.Tests.Infrastructure.TestBase.InitializeOnce() 位置 C:\ProgramData\Jenkins\.jenkins\workspace\UIDemo\Tests\Infrastructure\TestBase.cs:行号 112 --TearDown 在 Moshocart.Tests.Infrastructure.TestBase.LogToReports() 位置 C:\ProgramData\Jenkins\.jenkins\workspace\UIDemo\Tests\Infrastructure\TestBase.cs:行号 205 在 Moshocart.Tests.Infrastructure.TestBase.Quit() 位置 C:\ProgramData\Jenkins\.jenkins\workspace\UIDemo\Tests\Infrastructure\TestBase.cs:行号 125
修改后的初始化代码
var options = new EdgeOptions(); options.AddArgument("disable-gpu"); options.AddUserProfilePreference("intl.accept_languages", "en"); options.AddUserProfilePreference("disable-popup-blocking", "true"); options.AddUserProfilePreference("download.prompt_for_download", false); options.AddUserProfilePreference("download.directory_upgrade", true); options.AddUserProfilePreference("credentials_enable_service", false); options.AddUserProfilePreference("plugins.always_open_pdf_externally", true); options.AddUserProfilePreference("profile.password_manager_enabled", false); options.AddUserProfilePreference("pdfjs.disabled", true); options.AddArguments("--disable-backgrounding-occluded-windows"); options.AddArgument("--no-sandbox"); options.AddArgument("start-maximized"); var edge = new EdgeConfig(); new DriverManager().SetUpDriver(edge, VersionResolveStrategy.MatchingBrowser); Thread.Sleep(10000); var CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var NewDir = $"{CurrentDirectory}\\Edge"; string directoryPath = NewDir; string fileName = "msedgedriver.exe"; string filePath = SearchFile(directoryPath, fileName); string filePath1 = "cmd" + " " + filePath; if (!string.IsNullOrEmpty(filePath)) { Console.WriteLine("File found: " + filePath); } else { Console.WriteLine("File not found."); } static string SearchFile(string directoryPath, string fileName) { string[] files = Directory.GetFiles(directoryPath, fileName); if (files.Length > 0) { return files[0]; } string[] subDirectories = Directory.GetDirectories(directoryPath); foreach (string subDirectory in subDirectories) { string filePath = SearchFile(subDirectory, fileName); if (!string.IsNullOrEmpty(filePath)) { return filePath; } } return null; } Console.WriteLine(filePath1); LogTestOutput("Driver Started $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); Driver = new EdgeDriver(filePath1, options); LogTestOutput("Driver Ended &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); break;
解决方案
针对DevToolsActivePort文件不存在导致的Edge崩溃问题,可尝试以下修复步骤:
- 指定调试端口:在EdgeOptions中添加
--remote-debugging-port=9222参数,强制固定调试端口,避免端口分配失败:options.AddArgument("--remote-debugging-port=9222"); - 设置临时用户数据目录:添加
--user-data-dir参数,指定一个临时目录作为Edge的用户数据目录,规避权限或目录冲突:string tempDir = Path.Combine(Path.GetTempPath(), "EdgeTestProfile"); options.AddArgument($"--user-data-dir={tempDir}"); - 修复驱动路径错误:修改后的代码中
filePath1 = "cmd" + " " + filePath是错误用法,EdgeDriver构造函数需要传入驱动所在目录路径,而非带cmd的命令行。替换为:string driverDir = Path.GetDirectoryName(filePath); Driver = new EdgeDriver(driverDir, options); - 检查Jenkins权限:确保Jenkins服务有足够权限访问Edge安装目录和临时文件目录,必要时以管理员身份运行Jenkins。
- 验证版本匹配:使用
VersionResolveStrategy.MatchingBrowser确保msedgedriver版本与Edge浏览器完全一致,避免版本不兼容问题。
内容的提问来源于stack exchange,提问作者Divyaraj Dodia
相关产品推荐
相关产品推荐

