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

Spring项目RemoteWebDriver创建失败,无法加载ApplicationContext求助

Spring配置RemoteWebDriver遇到SessionNotCreatedException问题

配置代码

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.MalformedURLException;
import java.net.URL;

@Configuration
public class WebDriverLibrary {

//    @Value("${remote.url}")
//    public URL remoteUrl;

    @Bean
    public WebDriver getRemoteWebDriverForChrome() throws MalformedURLException {
//        WebDriverManager.chromedriver().setup();
//        return new RemoteWebDriver(remoteUrl, DesiredCapabilities.chrome());

        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setCapability("browserVersion", "74");
        chromeOptions.setCapability("platformName", "Windows 10");
        return new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), chromeOptions);
    }
}

报错信息

java.lang.IllegalStateException: 加载ApplicationContext失败
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为'landingPage'的Bean时出错:通过字段'webDriver'表达的依赖不满足;嵌套异常是org.springframework.beans.factory.BeanCreationException: 创建类路径资源[com/ksupwlt/stepcounttracker/libraries/WebDriverLibrary.class]中定义的名为'getRemoteWebDriverForChrome'的Bean时出错:通过工厂方法实例化Bean失败;嵌套异常是org.springframework.beans.BeanInstantiationException: 无法实例化[org.openqa.selenium.WebDriver]:工厂方法'getRemoteWebDriverForChrome'抛出异常;嵌套异常是org.openqa.selenium.SessionNotCreatedException: 无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败。
主机信息: host: 'DESKTOP-55B4PJT', ip: '192.168.1.66'
构建信息: version: '4.8.1', revision: '8ebccac989'
系统信息: os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '19.0.2'
驱动信息: org.openqa.selenium.remote.RemoteWebDriver

排查解决步骤

  • 确认Selenium Grid服务状态:检查http://localhost:4444/wd/hub对应的Selenium Hub是否正常启动。可以直接在浏览器访问该地址查看Grid控制台,或用curl http://localhost:4444/wd/hub/status测试连通性,返回正常JSON则服务运行正常。
  • 匹配浏览器与驱动版本:代码指定Chrome 74版本,需确认Grid节点上的Chrome版本确实为74,且对应ChromeDriver版本与Selenium 4.8.1兼容。Selenium 4.8.1对旧版本浏览器支持有限,建议升级Chrome到较新稳定版,或更换与Chrome 74匹配的Selenium版本。
  • 统一平台配置:代码设置platformName为Windows 10,但本地系统是Windows 11,需修改代码中的platformName为Windows 11,或确保Grid节点的平台配置与代码一致。
  • 检查网络与端口:确认本地防火墙、杀毒软件未拦截4444端口请求,保证本地能正常访问Grid服务地址。
  • 优化代码配置:将硬编码的服务地址、浏览器版本、平台信息移到配置文件,通过@Value注入方便维护:
    @Value("${remote.url}")
    private URL remoteUrl;
    
    @Value("${chrome.browser.version}")
    private String chromeVersion;
    
    @Value("${platform.name}")
    private String platformName;
    
    @Bean
    public WebDriver getRemoteWebDriverForChrome() throws MalformedURLException {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setCapability("browserVersion", chromeVersion);
        chromeOptions.setCapability("platformName", platformName);
        return new RemoteWebDriver(remoteUrl, chromeOptions);
    }
    
    同时在application.properties添加对应配置:
    remote.url=http://localhost:4444/wd/hub
    chrome.browser.version=74
    platform.name=Windows 11
    

内容的提问来源于stack exchange,提问作者Santhiya s

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 21:07:08