使用Selenium Grid运行测试遇‘Could not start a new session’错误求助
问题排查与解决方案
核心问题定位
你遇到的Could not start a new session错误,底层ImmutableMap方法缺失,本质是Guava版本冲突;而Chrome空白页、Firefox无法运行的问题,则和Selenium Grid版本兼容性、RemoteWebDriver配置有关。
分步解决
1. 修复Guava依赖冲突
Selenium Grid 4.8.3依赖Guava 31.1-jre,如果项目中存在其他版本的Guava(比如低版本),会覆盖Selenium所需的类,导致方法缺失。
- 在
pom.xml中显式声明Guava版本,强制使用兼容版本:<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>31.1-jre</version> </dependency> - 执行
mvn dependency:tree查看依赖树,找到冲突的Guava来源,在对应依赖中排除旧版本:<dependency> <!-- 替换为你的冲突依赖 --> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <version>xxx</version> <exclusions> <exclusion> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </exclusion> </exclusions> </dependency>
2. 升级Selenium Grid版本
Selenium 4.8.3发布于2023年2月,而Chrome 113是2023年4月发布的,两者存在兼容性缺口。建议升级到4.10.0及以上版本,该版本对Chrome 113+支持更完善:
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.10.0</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-grid</artifactId> <version>4.10.0</version> </dependency>
3. 修正RemoteWebDriver初始化配置
Chrome空白页显示data/,是因为Capabilities配置不规范,导致Grid节点无法正确识别浏览器参数。确保Base类中初始化代码正确:
ChromeOptions options = new ChromeOptions(); options.setCapability("browserVersion", "113"); options.setCapability("platformName", "Windows"); // 根据你的操作系统调整 WebDriver driver = new RemoteWebDriver(new URL("http://你的Grid Hub地址:4444"), options);
同时确认Grid Node启动时指定了正确的Driver路径:
java -jar selenium-server-4.10.0.jar node --driver-path="C:/path/to/chromedriver.exe" --port=5555
4. Firefox适配检查(可选)
如果需要用Firefox,需:
- 确保GeckoDriver版本与Firefox版本匹配
- Grid Node启动时指定GeckoDriver路径
- 初始化FirefoxOptions时配置正确的Capabilities
5. 清理重构项目
执行mvn clean install清理旧依赖缓存,重新下载正确版本的Jar包,避免残留文件干扰。
内容的提问来源于stack exchange,提问作者AptalKiz
相关产品推荐
相关产品推荐

