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

Selenium ChromeDriver无法加载Google.com的问题求助

Selenium ChromeDriver WebSocket连接失败问题排查

运行Java版Selenium代码尝试打开Chrome浏览器访问Google.com,但Chrome打开后地址栏无内容,同时出现WebSocket连接失败报错,已尝试更新ChromeDriver、添加pom依赖但问题仍未解决。

代码示例

import org.openqa.selenium.By;  
import org.openqa.selenium.JavascriptExecutor;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  

public class HelloWorldSelenium {  

    public static void main(String[] args) {  
       
           // System Property for Chrome Driver   
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Kamilia\\Downloads\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe");  
          
             // Instantiate a ChromeDriver class.      
        WebDriver driver= new ChromeDriver();  
          
           // Launch Website  
        driver.navigate().to("http://www.google.com/");  
          
         //Maximize the browser  
          driver.manage().window().maximize();              
    }    
}  

错误日志

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 126.0.6478.126 (d36ace6122e0a59570e258d82441395206d60e1c-refs/branch-heads/6478@{#1591}) on port 57956
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
juin 26, 2024 12:08:43 P.M. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
juin 26, 2024 12:08:43 P.M. org.openqa.selenium.remote.http.WebSocket$Listener onError
WARNING: Invalid Status code=403 text=Forbidden
java.io.IOException: Invalid Status code=403 text=Forbidden
    at org.asynchttpclient.netty.handler.WebSocketHandler.abort(WebSocketHandler.java:92)
    at org.asynchttpclient.netty.handler.WebSocketHandler.handleRead(WebSocketHandler.java:118)
    at org.asynchttpclient.netty.handler.AsyncHttpClientHandler.channelRead(AsyncHttpClientHandler.java:78)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324)
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:311)
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:432)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276)
    at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:719)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:1570)

Exception in thread "main" org.openqa.selenium.remote.http.ConnectionFailedException: Unable to establish websocket connection to http://localhost:61022/devtools/browser/bf66aa4a-459d-4e91-b1e4-12698a7dc404
Build info: version: '4.1.0', revision: '87802e897b'
System info: host: 'DESKTOP-BVFDT7U', ip: '192.168.0.37', os.name: 'Windows 11', os.arch: 'amd64', os.version: '10.0', java.version: '22'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.http.netty.NettyWebSocket.<init>(NettyWebSocket.java:104)
    at org.openqa.selenium.remote.http.netty.NettyWebSocket.lambda$create$3(NettyWebSocket.java:137)
    at org.openqa.selenium.remote.http.netty.NettyClient.openSocket(NettyClient.java:116)
    at org.openqa.selenium.devtools.Connection.<init>(Connection.java:77)
    at org.openqa.selenium.chromium.ChromiumDriver.lambda$new$2(ChromiumDriver.java:124)
    at java.base/java.util.Optional.map(Optional.java:260)
    at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:122)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:104)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:91)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:46)
    at HelloWorldSelenium.main(HelloWorldSelenium.java:14)

解决方案

  • 版本兼容性校验:确认Chrome浏览器版本与ChromeDriver版本完全一致,当前使用的ChromeDriver为126.0.6478.126,检查Chrome浏览器版本是否匹配,不匹配则重新下载对应版本的ChromeDriver。
  • 升级Selenium依赖:当前使用的Selenium版本为4.1.0,该版本与Java 22、Chrome 126存在兼容性冲突,升级至最新稳定版(如4.20+),更新pom.xml中的selenium-java依赖版本。
  • 禁用DevTools连接:初始化ChromeDriver时添加参数禁用DevTools,规避WebSocket连接问题,修改代码如下:
    import org.openqa.selenium.chrome.ChromeOptions;
    // ...
    ChromeOptions options = new ChromeOptions();
    options.setCapability("se:disableDevTools", true);
    WebDriver driver = new ChromeDriver(options);
    
  • 关闭系统代理:本地代理可能拦截WebSocket连接,关闭系统代理或为ChromeDriver添加无代理参数:
    options.addArguments("--no-proxy-server");
    
  • 使用独立Chrome配置文件:避免现有Chrome配置干扰,启动ChromeDriver时指定新的用户数据目录:
    options.addArguments("--user-data-dir=C:\\temp\\chrome-new-profile");
    
  • 管理员权限运行:Windows系统权限限制可能导致WebSocket连接失败,右键以管理员身份运行Java程序。

内容的提问来源于stack exchange,提问作者Kamilia Bouras

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 17:59:49