如何解决Selenium RemoteWebDriver的SessionNotCreatedException及Maven构建失败问题?
自动化测试问题排查:SessionNotCreatedException与Maven构建失败
问题背景
使用Selenium 4.1.0结合TestNG 7.4.0编写自动化测试代码,通过RemoteWebDriver连接localhost:4444远程服务器时抛出SessionNotCreatedException异常(请求超时);同时执行maven-surefire-plugin 3.0.0-M5时出现构建失败,怀疑插件版本存在问题。
测试代码
FirefoxOptions chromeOptions = new FirefoxOptions(); WebDriver driver = null; @BeforeTest public void beforeTest() throws Exception { driver = new RemoteWebDriver(new URL("http://localhost:4444"), chromeOptions); } @Test //Checking the title of iamNeo (Home - iamneo) public void iamNeo() throws InterruptedException { // driver.manage().window().maximize(); driver.navigate().to("http://iamneo.ai/"); String title = driver.getTitle(); System.out.println(title); Assert.assertEquals(title, "Learning and assessment solution for Universities and Enterprises"); } @Test //Moving to FACEBOOK public void nextPage() throws InterruptedException { driver.navigate().to("https://www.facebook.com"); String title = driver.getTitle(); Assert.assertEquals(title, "Facebook – log in or sign up"); } @Test //Back to iamNeo public void backPage() throws InterruptedException { driver.navigate().back(); String title = driver.getTitle(); System.out.println(title); Assert.assertEquals(title, "Learning and assessment solution for Universities and Enterprises"); } @Test //Current URL public void currentURL() throws InterruptedException { System.out.println(driver.getCurrentUrl()); String title = driver.getTitle(); System.out.println(title); Assert.assertEquals(title, "Learning and assessment solution for Universities and Enterprises"); driver.navigate().forward(); driver.navigate().refresh(); } @AfterTest public void afterTest() { driver.quit(); }
报错信息
It is Throwing Session Not created Exception : Could not start a new session. Possible causes are invalid address of the remote server or browser start-up : org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. Build info: version: '4.1.0', revision: '87802e897b' System info: host: 'eeeccebaefcebfacecbccfaabacdfdabdafecefecf-0', ip: '10.41.17.6', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.217+', java.version: '11.0.13' Driver info: org.openqa.selenium.remote.RemoteWebDriver Command: [null, newSession {capabilities=[Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:debuggerAddress: true, moz:firefoxOptions: {}}], desiredCapabilities=Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:debuggerAddress: true, moz:firefoxOptions: {}}}] Capabilities {} at ai.iamneo.testing.Testing_Selenium_TestNg.AppTest.beforeTest(AppTest.java:24) Caused by: java.lang.RuntimeException: NettyHttpHandler request execution error at ai.iamneo.testing.Testing_Selenium_TestNg.AppTest.beforeTest(AppTest.java:24) Caused by: java.util.concurrent.ExecutionException: java.util.concurrent.TimeoutException: Request timeout to localhost/127.0.0.1:4444 after 180000 ms at ai.iamneo.testing.Testing_Selenium_TestNg.AppTest.beforeTest(AppTest.java:24)
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ai.iamneo.testing</groupId> <artifactId>Testing-Selenium-TestNg</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Testing-Selenium-TestNg</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.1.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.4.0</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <compilerVersion>11</compilerVersion> </configuration> </plugin> </plugins> </build> </project>
问题分析与解决方案
一、解决SessionNotCreatedException(请求超时)
确认Selenium Grid服务状态
- 先启动Hub:
java -jar selenium-server-4.1.0.jar hub - 再启动Node:
java -jar selenium-server-4.1.0.jar node --hub http://localhost:4444 - 访问
http://localhost:4444验证Grid控制台是否正常打开,确保服务监听4444端口。
- 先启动Hub:
修正代码变量命名(规范优化)
代码中FirefoxOptions的变量名是chromeOptions,易造成混淆,建议修正:FirefoxOptions firefoxOptions = new FirefoxOptions(); WebDriver driver = null; @BeforeTest public void beforeTest() throws Exception { driver = new RemoteWebDriver(new URL("http://localhost:4444"), firefoxOptions); }检查浏览器驱动兼容性
- 确保GeckoDriver版本与Firefox浏览器、Selenium 4.1.0匹配
- 若系统PATH未包含GeckoDriver,可在代码中指定路径:
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
排查网络与端口连通性
- 用
telnet localhost 4444或nc -zv localhost 4444测试端口是否可访问 - 检查防火墙是否拦截4444端口通信
- 用
二、解决Maven Surefire插件构建失败
3.0.0-M5是Surefire插件的里程碑版本,稳定性和兼容性不足,建议替换为稳定版:
更新pom.xml插件版本
将插件版本改为稳定版(如3.2.5):<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.2.5</version> <configuration> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin>重新构建测试
执行mvn clean test验证构建是否成功,稳定版插件与TestNG 7.4.0兼容性更好,可避免里程碑版本的未知bug。
内容的提问来源于stack exchange,提问作者Dharma176
相关产品推荐
相关产品推荐

