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

使用Eclipse+Maven+JUnit时Selenium无法找到Chrome Binary

解决Selenium无法找到Chrome Binary的问题

核心问题分析

报错unknown error: cannot find Chrome binary说明ChromeDriver能正常启动,但无法定位到Chrome浏览器的可执行文件,常见原因包括浏览器路径非默认、驱动与浏览器版本不兼容、手动配置的驱动路径无效。


针对性解决方案

1. 指定Chrome浏览器的二进制路径

如果Chrome安装在自定义目录(而非系统默认位置),需在代码中明确指定Chrome.exe的路径:

package com.qualitystream.tutorial;

import static org.junit.Assert.assertEquals;

import java.time.Duration;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class GoogleSearchTest {
    
    private WebDriver driver;
    
    @Before
    public void setUp() throws Exception {
        ChromeOptions options = new ChromeOptions();
        // 替换为你本地Chrome.exe的实际路径
        options.setBinary("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe");
        // 若使用手动下载的驱动,保留此行;后续推荐用WebDriverManager可删除
        System.setProperty("webdriver.chrome.driver", "./src/test/resources/chromedriver/chromedriver.exe");
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        driver.get("https://www.google.com");
    }

    @Test
    public void testGooglePage() {
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.clear();
        searchBox.sendKeys("quality-stream Introducción a la Automatización de Pruebas de Software");
        searchBox.submit();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
        assertEquals("quality-stream Introducción a la Automatización de Pruebas de Software - Google Search",driver.getTitle());
    }
    
    @After
    public void tearDown() {
        driver.quit(); // 恢复此行,避免残留Chrome进程
    }
}

2. 修复ChromeDriver与Chrome版本不兼容问题

日志显示你使用的ChromeDriver是114版本,但Selenium 4.17.0对应的Chrome版本应为121.x左右,版本不匹配会导致启动失败:

  • 卸载现有ChromeDriver,下载与本地Chrome版本完全一致的驱动文件替换
  • 推荐使用WebDriverManager自动管理驱动(见下文),彻底避免版本匹配问题

3. 用WebDriverManager自动管理驱动(推荐)

无需手动下载和配置驱动路径,WebDriverManager会自动检测Chrome版本并下载匹配的驱动:

  1. 在POM.xml中添加依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.qualitystream.tutorial</groupId>
  <artifactId>QualityStreamTutorial</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.17.0</version>
        </dependency>

        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>net.minidev</groupId>
            <artifactId>accessors-smart</artifactId>
            <version>2.5.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version> <!-- 改用稳定版,移除beta版本 -->
            <scope>test</scope>
        </dependency>
  </dependencies>
</project>
  1. 修改测试类的setUp方法,删除手动配置驱动路径的代码:
@Before
public void setUp() throws Exception {
    WebDriverManager.chromedriver().setup();
    ChromeOptions options = new ChromeOptions();
    // 若Chrome不在默认路径,依然可以用options.setBinary指定路径
    driver = new ChromeDriver(options);
    driver.manage().window().maximize();
    driver.get("https://www.google.com");
}

4. 验证驱动路径有效性

若坚持手动配置驱动,需确认./src/test/resources/chromedriver/chromedriver.exe的路径在Maven运行时能被正确识别,可改用绝对路径排查:

System.setProperty("webdriver.chrome.driver", "D:\\YourProjectPath\\src\\test\\resources\\chromedriver\\chromedriver.exe");

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 16:40:41