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

Selenium自动化测试Chrome Driver异常:页面关闭及日志报错问题

问题解决指南

1. 页面自动关闭问题

  • 原因:测试执行完成后驱动实例被销毁,或程序主线程直接退出,导致Chrome进程终止。
  • 解决方案:
    • 在测试代码末尾添加等待逻辑(如Thread.sleep(5000)),或等待用户输入后再调用driver.quit();
    • 确保驱动实例生命周期与测试流程匹配,避免提前被垃圾回收;
    • 调试时可通过ChromeOptions配置让浏览器窗口保持存活:
      ChromeOptions options = new ChromeOptions();
      options.setExperimentalOption("detach", true); // 调试用,生产环境不建议保留
      WebDriver driver = new ChromeDriver(options);
      

2. SLF4J日志缺失问题

  • 原因:项目缺少SLF4J的具体实现依赖,无法输出日志。
  • 解决方案:在Maven依赖中添加SLF4J实现,例如slf4j-simple:
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.7</version>
        <scope>test</scope>
    </dependency>
    

3. CDP版本不匹配警告

  • 原因:当前Selenium 4.10.0最高支持CDP 114版本,但你的Chrome浏览器为116版本,存在兼容性差异。
  • 解决方案:
    • 升级Selenium Java版本至4.11.0或更高(该版本支持CDP 116);
    • 同步升级WebDriverManager至5.6.0或以上,确保与Selenium版本兼容。

4. Maven依赖优化

  • 问题:依赖中存在重复的selenium-api声明(同时设置了test和compile scope),而selenium-java已包含selenium-api,无需单独添加。
  • 优化后的完整依赖配置:
    <properties>
        <maven.compiler.source>20</maven.compiler.source>
        <maven.compiler.target>20</maven.compiler.target>
        <webdriver.manager.version>5.6.0</webdriver.manager.version>
        <selenium.java.version>4.11.0</selenium.java.version>
        <testng.version>7.8.0</testng.version>
        <browser>chrome</browser>
        <base.url>https://www.saucedemo.com/v1/</base.url>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>${webdriver.manager.version}</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>${selenium.java.version}</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
        </dependency>
        <!-- 添加SLF4J日志实现 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>2.0.7</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 20:55:26