Selenide自动化测试报错:无法建立WebSocket连接
Selenide自动化测试WebSocket连接失败问题解决
问题描述
使用Selenide编写自动化测试时,出现浏览器无法与http://localhost:2757/devtools/browser/uuid建立WebSocket连接的异常,错误日志如下:
[ERROR] Tests run: 97, Failures: 1, Errors: 0, Skipped: 96, Time elapsed: 3.945 s <<< FAILURE! - in com.rev.testManager.GenericTestManager [ERROR] com.rev.testManager.GenericTestManager.openPage Time elapsed: 3.727 s <<< FAILURE! org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection to http://localhost:2757/devtools/browser/d5f72b05-f030-41ee-88b2-5823e7d0f35c Build info: version: '4.4.0', revision: 'e5c75ed026a' System info: host: 'localhost', ip: '127.0.0.1', os.name: 'Linux', os.arch: 'amd64', os.version: '5.14.0-283.el9.x86_64', java.version: '18.0.2' Driver info: driver.version: SelenideDriver selenide.url: https://www.buletintv3.my/ selenide.baseUrl: https://www.buletintv3.my/ at org.openqa.selenium.remote.http.netty.NettyWebSocket.<init>(NettyWebSocket.java:102) at org.openqa.selenium.remote.http.netty.NettyWebSocket.lambda$create$3(NettyWebSocket.java:128) at org.openqa.selenium.remote.http.netty.NettyClient.openSocket(NettyClient.java:106) at org.openqa.selenium.devtools.Connection.<init>(Connection.java:77) at org.openqa.selenium.chromium.ChromiumDriver.lambda$new$2(ChromiumDriver.java:116) at java.base/java.util.Optional.map(Optional.java:260) at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:114) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:81) at com.codeborne.selenide.webdriver.ChromeDriverFactory.create(ChromeDriverFactory.java:42) at com.codeborne.selenide.webdriver.WebDriverFactory.createWebDriverInstance(WebDriverFactory.java:109) at com.codeborne.selenide.webdriver.WebDriverFactory.createWebDriver(WebDriverFactory.java:69) at com.codeborne.selenide.drivercommands.CreateDriverCommand.lambda$createDriver$0(CreateDriverCommand.java:76) at com.codeborne.selenide.logevents.SelenideLogger.get(SelenideLogger.java:166) at com.codeborne.selenide.drivercommands.CreateDriverCommand.createDriver(CreateDriverCommand.java:52) at com.codeborne.selenide.impl.WebDriverThreadLocalContainer.createDriver(WebDriverThreadLocalContainer.java:180) at com.codeborne.selenide.impl.WebDriverThreadLocalContainer.getAndCheckWebDriver(WebDriverThreadLocalContainer.java:149) at com.codeborne.selenide.WebDriverRunner.getAndCheckWebDriver(WebDriverRunner.java:130) at com.codeborne.selenide.impl.StaticDriver.getAndCheckWebDriver(StaticDriver.java:65) at com.codeborne.selenide.SelenideDriver.getAndCheckWebDriver(SelenideDriver.java:220) at com.codeborne.selenide.drivercommands.Navigator.lambda$navigateTo$0(Navigator.java:72) at com.codeborne.selenide.logevents.SelenideLogger.run(SelenideLogger.java:153) at com.codeborne.selenide.drivercommands.Navigator.navigateTo(Navigator.java:70) at com.codeborne.selenide.drivercommands.Navigator.open(Navigator.java:32) at com.codeborne.selenide.SelenideDriver.open(SelenideDriver.java:101) at com.codeborne.selenide.Selenide.open(Selenide.java:52) at com.rev.testManager.GenericTestManager.openPage(GenericTestManager.java:27) at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) at java.base/java.lang.reflect.Method.invoke(Method.java:577) at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139) at org.testng.internal.invokers.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:69) at org.testng.internal.invokers.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:361) at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:296) at org.testng.internal.invokers.TestInvoker.runConfigMethods(TestInvoker.java:816) at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:590) at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:221) at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50) at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:962) at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:194) at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:148) at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at org.testng.TestRunner.privateRun(TestRunner.java:806) at org.testng.TestRunner.run(TestRunner.java:601) at org.testng.SuiteRunner.runTest(SuiteRunner.java:433) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:427) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:387) at org.testng.SuiteRunner.run(SuiteRunner.java:330) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1256) at org.testng.TestNG.runSuitesLocally(TestNG.java:1176) at org.testng.TestNG.runSuites(TestNG.java:1099) at org.testng.TestNG.run(TestNG.java:1067) at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:151) at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeSingleClass(TestNGDirectoryTestSuite.java:111) at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:98) at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:155) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:456) at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:169) at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:595) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:581)
解决方案
1. 匹配ChromeDriver与Chrome版本兼容性
Selenium 4.4.0对ChromeDriver和Chrome浏览器版本有严格匹配要求,版本不兼容会直接导致DevTools连接失败:
- 查看本地Chrome浏览器版本,下载对应版本的ChromeDriver替换现有驱动
- 使用WebDriverManager自动管理驱动版本,避免手动匹配出错
2. 禁用DevTools连接
如果测试不需要DevTools功能,可以直接禁用该连接尝试:
import org.openqa.selenium.chrome.ChromeOptions; import com.codeborne.selenide.Configuration; import java.util.Collections; public class TestConfig { static { ChromeOptions options = new ChromeOptions(); options.addArguments("--disable-devtools"); options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation")); Configuration.browserCapabilities = options; } }
3. 检查并释放端口占用
localhost:2757端口可能被其他进程占用,导致ChromeDriver无法正常监听:
- Linux系统执行
lsof -i :2757查看占用进程,用kill -9 <PID>结束进程 - Windows系统执行
netstat -ano | findstr :2757找到进程ID,通过任务管理器结束 - 也可以指定ChromeDriver使用其他端口:
ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-debugging-port=9222"); Configuration.browserCapabilities = options;
4. 升级Selenide和Selenium版本
Selenium 4.4.0存在WebSocket连接的已知bug,升级到较新版本可修复:
修改pom.xml中的依赖版本(示例为当前稳定版):
<dependency> <groupId>com.codeborne</groupId> <artifactId>selenide</artifactId> <version>6.19.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.15.0</version> <scope>test</scope> </dependency>
5. 检查代理设置
如果测试环境使用了代理,可能会阻断WebSocket连接:
ChromeOptions options = new ChromeOptions(); options.addArguments("--no-proxy-server"); Configuration.browserCapabilities = options;
内容的提问来源于stack exchange,提问作者Ivan
相关产品推荐
相关产品推荐

