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

Java环境下Selenium+ChromeDriver无法打开网页问题求助

问题解决方案

针对你遇到的Chrome浏览器能启动但无法打开网页、WebSocket连接403失败的问题,可尝试以下方案:

1. 添加ChromeDriver Origin允许参数

日志里的403 Forbidden和WebSocket连接失败,大概率是ChromeDriver的安全限制导致的。给ChromeOptions添加--remote-allow-origins=*参数,允许本地连接的跨域请求:

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class TestClass {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);
        driver.get("http://google.com/");
    }
}

2. 改用Selenium Manager自动管理驱动

Selenium 4.6及以上版本自带Selenium Manager,无需手动设置webdriver.chrome.driver路径,它会自动下载匹配版本的ChromeDriver,避免版本不匹配或路径错误:

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class TestClass {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);
        driver.get("http://google.com/");
    }
}

(注意:删除原来的System.setProperty代码即可)

3. 排查代理设置

如果你的系统开启了代理,可能会拦截ChromeDriver的WebSocket连接:

  • 暂时关闭系统代理再测试
  • 或者给ChromeOptions添加--no-proxy-server参数,让Chrome跳过代理:
options.addArguments("--no-proxy-server");

4. 确认Chrome与ChromeDriver版本完全匹配

虽然你提到用了对应版本,但再次确认:Chrome浏览器版本(通过chrome://version/查看)需和ChromeDriver版本完全一致,比如Chrome 111.0.5563.xxx对应ChromeDriver 111.0.5563.xxx。

另外,日志里的SLF4J警告不影响核心功能,若要消除,可在项目依赖中添加SLF4J的实现包(如slf4j-simple)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 22:53:00