Debian服务器Selenium启动Chrome报错:无法找到Chrome binary求助
问题
在Debian服务器上已安装Google Chrome 121.0.6167.139版本,通过Azure DevOps执行UI测试时出现错误:
Could not start a new session. Response code 500. Message: unknown error: cannot find Chrome binary
驱动管理代码
public static WebDriver getDriver() { if (driverPool.get() == null) { String browserType = ConfigurationReader.getProperty("browser"); switch (browserType) { case "chrome": WebDriverManager.chromedriver().setup(); chromeOptions.addArguments("--remote-allow-origins=*"); chromeOptions.addArguments("incognito"); driverPool.set(new ChromeDriver(chromeOptions)); chromeOptions.addArguments("window-size=1920,1080"); driverPool.get().manage().window().maximize(); driverPool.get().manage().timeouts().implicitlyWait(Duration.ofSeconds(15)); break; case "firefox": WebDriverManager.firefoxdriver().setup(); chromeOptions.addArguments("--remote-allow-origins=*"); chromeOptions.addArguments("incognito"); driverPool.set(new FirefoxDriver(firefoxOptions)); driverPool.get().manage().window().maximize(); driverPool.get().manage().timeouts().implicitlyWait(Duration.ofSeconds(15)); case "headless-chrome": WebDriverManager.chromedriver().setup(); chromeOptions.addArguments("--remote-allow-origins=*"); chromeOptions.addArguments("incognito"); chromeOptions.setHeadless(true); driverPool.set(new ChromeDriver(chromeOptions)); driverPool.get().manage().window().maximize(); driverPool.get().manage().timeouts().implicitlyWait(Duration.ofSeconds(15)); break; } } return driverPool.get(); }
完整错误日志
[ERROR] Security Transactions Security Transfer İşlemi #3.Security Transactions Customer Transactions Out Input İşlemi Time elapsed: 0 s <<< ERROR! org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: cannot find Chrome binary Host info: host: 'debian', ip: '127.0.1.1' Build info: version: '4.12.1', revision: '8e34639b11' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.0-22-amd64', java.version: '11.0.18' Driver info: org.openqa.selenium.chrome.ChromeDriver Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [incognito, --remote-allow-origins=*, incognito], extensions: []}}]}]
依赖配置
<dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.4.1</version> </dependency> <!-- Selenium --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.12.1</version> </dependency>
使用环境:Selenium WebDriver 4.12.2、Java 11、bonigarcia 5.4.1,需解决Debian服务器上启动Chrome的问题,确认是否需要额外库。
解决方案
1. 确认Chrome二进制文件路径
Debian上安装的Chrome默认路径通常为/usr/bin/google-chrome,先通过命令验证实际路径:
which google-chrome
2. 在代码中指定Chrome二进制路径
修改ChromeOptions配置,明确告知WebDriver Chrome的位置,避免查找失败:
// 在chromeOptions初始化后添加路径配置 chromeOptions.setBinary("/usr/bin/google-chrome"); // 替换为实际查询到的路径
修改后的chrome分支示例(同时修复参数顺序问题):
case "chrome": WebDriverManager.chromedriver().setup(); chromeOptions = new ChromeOptions(); // 初始化独立实例,避免参数污染 chromeOptions.setBinary("/usr/bin/google-chrome"); chromeOptions.addArguments("--remote-allow-origins=*"); chromeOptions.addArguments("incognito"); chromeOptions.addArguments("window-size=1920,1080"); // 移到创建Driver前,确保生效 driverPool.set(new ChromeDriver(chromeOptions)); driverPool.get().manage().window().maximize(); driverPool.get().manage().timeouts().implicitlyWait(Duration.ofSeconds(15)); break;
3. 修复代码中的其他错误
- Firefox分支问题:当前错误使用
chromeOptions配置Firefox,需替换为firefoxOptions,同时添加break语句避免执行后续分支:
case "firefox": WebDriverManager.firefoxdriver().setup(); firefoxOptions = new FirefoxOptions(); firefoxOptions.addArguments("--remote-allow-origins=*"); firefoxOptions.addArguments("-private"); // Firefox隐私模式参数 driverPool.set(new FirefoxDriver(firefoxOptions)); driverPool.get().manage().window().maximize(); driverPool.get().manage().timeouts().implicitlyWait(Duration.ofSeconds(15)); break;
- 避免Options实例共享:每个浏览器分支都要初始化独立的Options对象,防止不同浏览器的参数互相干扰。
4. 安装Debian服务器所需依赖库
Debian无图形界面环境下运行Chrome需要依赖系统库,执行以下命令安装:
apt-get update && apt-get install -y \ libxss1 \ libnss3 \ libgconf-2-4 \ xvfb
其中xvfb用于提供虚拟显示环境,确保无图形界面时Chrome能正常启动。
5. 锁定ChromeDriver版本匹配Chrome
确保WebDriverManager下载的ChromeDriver版本与已安装的Chrome 121.0.6167.139完全匹配,可在代码中指定版本:
WebDriverManager.chromedriver().browserVersion("121.0.6167.139").setup();
内容的提问来源于stack exchange,提问作者Mehmet Usta
相关产品推荐
相关产品推荐

