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

如何解决Selenium中开启不同端口重复浏览器的403 Forbidden错误

解决Selenium连接Chrome调试端口出现「Invalid Status code=403 text=Forbidden」错误

问题场景

通过Selenium的debuggerAddress参数连接指定端口的Chrome实例时,触发403禁止访问错误,错误日志如下:

ChromeDriver was started successfully.
Mar 14, 2023 12:13:48 PM 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:444)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
    at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:336)
    at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:323)
    at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:444)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:280)
    at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
    at io.netty.handler.logging.LoggingHandler.channelRead(LoggingHandler.java:280)

当前用于连接的代码:

public static void chromeSetup() {
    WebDriverManager.chromedriver().setup();
    ChromeOptions opt = new ChromeOptions();

    opt.setExperimentalOption("debuggerAddress", "localhost:9988");
    driver = new ChromeDriver(opt);
    
}

调用场景示例:

if (reportType.equals("Y") || reportType.equals("y")) {
    chromeSetup();
    driver.get("sample url");
    exportButton();

    mainKey();

} else if (reportType.equals("N") || reportType.equals("n")) {

    mainKey();

}

核心原因

这个403错误主要是新版Chrome默认限制了远程调试的跨域访问,如果启动Chrome时没有配置允许调试请求的来源,Selenium的WebSocket连接会被拒绝。另外也可能是Chrome实例未用正确参数启动,或者端口未被正确监听。

解决办法

1. 用正确参数启动Chrome实例

手动或通过代码启动Chrome时,必须添加以下参数:

  • --remote-debugging-port=9988:指定调试端口(和代码中的debuggerAddress端口一致)
  • --remote-allow-origins=*:允许所有来源的调试请求(新版Chrome必填,否则会触发403)
  • 可选:--no-first-run --no-default-browser-check:跳过首次启动检查

示例启动命令(Windows):

chrome.exe --remote-debugging-port=9988 --remote-allow-origins=*

如果是通过Java代码启动Chrome进程,可以这样写:

// 构造Chrome启动命令
ProcessBuilder pb = new ProcessBuilder("chrome.exe", "--remote-debugging-port=9988", "--remote-allow-origins=*");
pb.start();
// 等待Chrome启动完成
Thread.sleep(3000);

2. 修正Selenium连接代码

确保代码中仅连接已启动的Chrome实例,避免重复启动ChromeDriver导致冲突:

public static void chromeSetup() {
    WebDriverManager.chromedriver().setup();
    ChromeOptions opt = new ChromeOptions();

    opt.setExperimentalOption("debuggerAddress", "localhost:9988");
    // 直接连接已有实例,不需要重新启动Chrome
    driver = new ChromeDriver(opt);
}

3. 版本匹配检查

确保ChromeDriver版本与本地Chrome浏览器版本完全一致,版本不匹配也可能导致调试连接异常。可以通过WebDriverManager自动匹配,或者手动下载对应版本的ChromeDriver。

4. 端口占用检查

确认9988端口未被其他程序占用,可通过以下命令排查:

  • Windows:netstat -ano | findstr :9988
  • Linux/macOS:lsof -i :9988

如果端口被占用,更换其他未被使用的端口即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 04:54:54