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

如何解决BDD-Cucumber中ChromeDriver路径重复引发的IllegalStateException?

问题解决方法

错误原因

从报错信息能直接看出,你拼接的ChromeDriver路径出现了重复叠加:

java.lang.IllegalStateException: The driver executable must exist: D:\Cucumber_BDD\CucumberJavaD:\Cucumber_BDD\CucumberJava\src\test\resources\drivers\chromedriver.exe

这是因为System.getProperty("user.dir")已经返回了项目根路径D:\Cucumber_BDD\CucumberJava,你又在后面拼接了完整的绝对路径,导致两个路径强制连在一起,生成的路径完全无效,自然找不到驱动文件。

修复方案

方案1:使用相对路径拼接(推荐,兼容性强)

利用项目根路径拼接相对路径,同时用File.separator适配Windows、Linux等不同系统的路径分隔符,避免硬编码斜杠带来的兼容问题:

修改后的代码:

import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class GoogleSearchSteps {
    WebDriver driver = null;

    @Given("browser is open")
    public void browser_is_open() {
        System.out.println("Inside Step - browser is open");
        
        String projectPath = System.getProperty("user.dir");
        System.out.println("Project path is" + projectPath);
        
        // 用相对路径拼接,通过File.separator适配系统
        String driverPath = projectPath + File.separator + "src" + File.separator + "test" + File.separator + "resources" + File.separator + "drivers" + File.separator + "chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", driverPath);
        
        driver = new ChromeDriver();
        
        driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        
        driver.manage().window().maximize();
    }
}

方案2:直接使用正确的绝对路径(不推荐,移植性差)

如果只是临时解决问题,不想用相对路径,直接写正确的绝对路径即可,不需要拼接projectPath:

System.setProperty("webdriver.chrome.driver", "D:/Cucumber_BDD/CucumberJava/src/test/resources/drivers/chromedriver.exe");

额外注意事项

  • 确认chromedriver.exe确实存在于你指定的路径中,且驱动版本和本地Chrome浏览器版本匹配(版本不兼容也会引发类似错误)
  • 若项目允许,可尝试使用WebDriverManager工具自动管理驱动,彻底避免手动配置路径的麻烦

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 16:45:53