Linux环境下Chrome Selenium WebDriver报错:权限拒绝/DevToolsActivePort不存在
Linux环境下C# Selenium Chrome启动失败排查方案
原始登录脚本
IWebDriver WebDriver = new ChromeDriver(); WebDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500); WebDriver.Navigate().GoToUrl("https://mysite.de/"); // Login var tenantTextbox = WebDriver.FindElement(By.Id("tenantTextbox")); var userTextTextbox = WebDriver.FindElement(By.Id("userTextTextbox")); var passwordTextbox = WebDriver.FindElement(By.Id("passwordTextbox")); var loginButton = WebDriver.FindElement(By.ClassName("loginbutton")); tenantTextbox.SendKeys("Tenant"); userTextTextbox.SendKeys("sysadmin"); passwordTextbox.SendKeys("password"); loginButton.Click();
已遇到的错误及处理
错误1:Selenium Manager权限不足
执行dotnet --roll-forward Major /srv/publish/UiTester.dll时抛出权限错误:
Unhandled exception. OpenQA.Selenium.NoSuchDriverException: Unable to obtain chrome using Selenium Manager; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors/driver_location ---> OpenQA.Selenium.WebDriverException: Error starting process: /srv/publish/selenium-manager/linux/selenium-manager --browser "chrome" --output json ---> System.ComponentModel.Win32Exception (13): An error occurred trying to start process '/srv/publish/selenium-manager/linux/selenium-manager' with working directory '/srv'. Permission denied
处理:执行chmod +x /srv/publish/selenium-manager/linux/selenium-manager赋予执行权限后解决。
错误2:Chrome启动失败(DevToolsActivePort文件不存在)
权限问题解决后,出现新错误:
Unhandled exception. System.InvalidOperationException: session not created: Chrome failed to start: exited normally. (session not created: DevToolsActivePort file doesn't exist) (The process started from chrome location /root/.cache/selenium/chrome/linux64/120.0.6099.109/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) (SessionNotCreated) at OpenQA.Selenium.WebDriver.UnpackAndThrowOnError(Response errorResponse, String commandToExecute) at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters) at OpenQA.Selenium.WebDriver.StartSession(ICapabilities desiredCapabilities) at OpenQA.Selenium.WebDriver..ctor(ICommandExecutor executor, ICapabilities capabilities) at OpenQA.Selenium.Chromium.ChromiumDriver..ctor(ChromiumDriverService service, ChromiumOptions options, TimeSpan commandTimeout) at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout) at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
尝试过的无效方案:组合设置--headless、--remote-debugging-port=60000、--no-sandbox、--disable-dev-shm-usage等Chrome启动参数,未解决问题;单独设置--remote-debugging-port=60000时提示无法设置调试端口。
额外排查方向
1. 检查Chrome安装完整性
Selenium Manager自动下载的Chrome可能存在文件损坏或依赖缺失,手动安装系统适配版本的Chrome/Chromium:
- 执行
sudo apt update && sudo apt install chromium-browser安装系统版本 - 配置ChromeOptions指定本地Chrome路径:
var options = new ChromeOptions(); options.BinaryLocation = "/usr/bin/chromium-browser"; // 根据实际路径调整 options.AddArgument("--headless=new"); // Selenium 4+推荐使用新版无头模式参数 options.AddArgument("--no-sandbox"); options.AddArgument("--disable-dev-shm-usage"); IWebDriver driver = new ChromeDriver(options);
2. 调整用户权限与运行环境
- 避免以root用户运行脚本:root用户下Chrome沙箱机制受限,创建普通用户并赋予
publish目录权限,切换用户后执行 - 若必须用root运行,检查
/root/.cache/selenium目录权限,确保该目录及子文件有读写执行权限
3. 修正Chrome启动参数
- 替换
--headless为--headless=new:旧无头模式在新版Chrome中存在兼容性问题 - 移除
--remote-debugging-port:无桌面环境的无头模式下该参数易引发冲突,无需调试可直接删除 - 添加
--disable-gpu参数:部分Linux环境下禁用GPU可解决启动异常 - 添加
--window-size=1920,1080:无头模式下指定窗口尺寸,避免默认尺寸导致启动失败
4. 验证.NET版本兼容性
- 尝试在服务器安装.NET 7 runtime,直接执行
dotnet /srv/publish/UiTester.dll,规避跨版本兼容隐性问题 - 重新发布项目时添加
--self-contained true参数,将.NET runtime打包到发布目录,不依赖服务器环境
5. 查看Chrome启动日志
- 配置ChromeDriver输出详细日志定位问题:
var service = ChromeDriverService.CreateDefaultService(); service.LogPath = "/tmp/chromedriver.log"; service.EnableVerboseLogging = true; var options = new ChromeOptions(); // 添加必要启动参数 IWebDriver driver = new ChromeDriver(service, options); - 查看
/tmp/chromedriver.log日志内容,查找Chrome启动时的具体错误信息
6. 检查系统依赖库
Chrome在Linux下依赖部分系统库,执行ldd /root/.cache/selenium/chrome/linux64/120.0.6099.109/chrome查看缺失依赖,安装对应库(例如libnss3、libxss1等)
版本信息
- Linux: 22.04.3 LTS (Jammy Jellyfish)
- Chromium driver版本: 120.0.6099.109(Selenium.WebDriver 4.16.*支持该版本)
- Selenium.WebDriver (NuGet): 4.16.2
- Dotnet (服务器): 8.0.100
- Dotnet (项目): 7.0.404
内容的提问来源于stack exchange,提问作者FranzHuber23
相关产品推荐
相关产品推荐

