使用WebDriverManager 5.6.0启动Chrome失败,TestNG测试报SessionNotCreatedException
运行TestNG测试时遇到以下错误:
org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Host info: host: 'xxxxx', ip: 'xxxxxxxxxxx'
Build info: version: '4.15.0', revision: '1d14b5521b'
System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '13.5', java.version: '21.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [null, newSession {capabilities=[Capabilities {browserName: chrome, goog:chromeOptions: {args: [], binary: /Application"
使用的版本详情
- Chrome浏览器版本:119
- Webdrivermanager:5.6.0
- TestNg版本:7.8.0
- Selenium-Java:4.15.0
- JDK:尝试过21、18和11版本,均出现相同问题
测试代码
import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.testng.annotations.*; public class okta1 { private WebDriver driver; @BeforeClass static void setupClass() { WebDriverManager.chromedriver().clearDriverCache().setup(); } @BeforeTest void setupTest() { ChromeOptions options = new ChromeOptions(); options.setCapability("browserVersion", "119.0.6045.105"); driver = new ChromeDriver(options); } @AfterTest void teardown() { driver.quit(); } @Test void test() { // Exercise driver.get("https://bonigarcia.dev/selenium-webdriver-java/"); String title = driver.getTitle(); // Verify // assertThat(title).contains("Selenium WebDriver"); } }
pom.xml配置
<?xml version="1.0" encoding="UTF-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>Teams-Automation-Framework</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>21</java.version> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.15.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.8.0</version> <scope>test</scope> </dependency> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.6.0</version> <scope>test</scope> </dependency> </dependencies> </project>
已尝试更换不同版本的JDK和WebDriverManager,问题依旧。
1. 修复Chrome版本配置错误
browserVersion参数无需指定完整版本号(如119.0.6045.105),只需主版本号119即可,或者直接移除该配置,WebDriverManager会自动匹配对应驱动版本。修改后的setupTest方法:
@BeforeTest void setupTest() { ChromeOptions options = new ChromeOptions(); // 可选:仅指定主版本 // options.setBrowserVersion("119"); driver = new ChromeDriver(options); }
2. 补全Chrome浏览器路径
错误日志显示binary: /Application,路径不完整。Mac系统下Chrome默认路径为/Applications/Google Chrome.app/Contents/MacOS/Google Chrome,可显式指定:
@BeforeTest void setupTest() { ChromeOptions options = new ChromeOptions(); options.setBinary("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"); driver = new ChromeDriver(options); }
3. 升级WebDriverManager版本
WebDriverManager 5.6.0与Selenium 4.15.0可能存在兼容性问题,建议升级到最新稳定版(如5.7.0+),修改pom.xml依赖:
<dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.7.0</version> <scope>test</scope> </dependency>
4. 清理浏览器进程与缓存
- 关闭所有Chrome窗口,在活动监视器中结束
Google Chrome相关后台进程 - 清理Chrome缓存和Cookie后重新运行测试
5. 验证驱动与浏览器版本匹配
手动确认WebDriverManager下载的驱动版本:
- 调用
WebDriverManager.chromedriver().getDownloadedDriverPath()查看驱动路径,检查版本是否为119.x.x.x - 若版本不匹配,删除WebDriverManager缓存目录(默认在
~/.cache/selenium),重新运行测试
内容的提问来源于stack exchange,提问作者Nabeel khan

