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

Selenium 3.14.0中ApacheHttpClient弃用,替代方案是什么?

替代弃用的ApacheHttpClient的方案

嘿,这个问题我刚好碰到过,来给你梳理下解决方案!你遇到的情况是Selenium 3.14.0中org.openqa.selenium.remote.internal.ApacheHttpClient被弃用,且在3.141.59版本中完全移除。官方推荐的替代方案是使用OkHttp客户端,这也是Selenium后续版本默认使用的HTTP客户端实现,性能和稳定性都更好。

具体步骤和代码替换

1. 升级Selenium版本(推荐)

首先建议你把Selenium版本升级到3.141.59(这是Selenium 3的最后一个稳定LTS版本),因为旧版本的弃用类在这个版本已经被彻底移除,同时OkHttp的实现也更加完善。

2. 替换客户端工厂和执行器代码

你原来的代码中使用ApacheHttpClient.Factory来配置超时,现在可以用OkHttp的客户端来替代。下面是修改后的完整代码片段:

import okhttp3.OkHttpClient;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriverService;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.internal.OkHttpClient;

import java.io.File;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

public class EdgeDriverSetup {
    public static void main(String[] args) {
        File edgeDriver = new File("path/to/your/msedgedriver.exe");
        EdgeDriverService edgeDriverService;
        WebDriver driver;

        final int connectionTimeout = 2 * 60 * 1000; 
        final int socketTimeout = 10 * 60 * 1000; // 10 minute timeout 

        // 1. 创建自定义配置的OkHttpClient,设置超时时间
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(connectionTimeout, TimeUnit.MILLISECONDS)
                .readTimeout(socketTimeout, TimeUnit.MILLISECONDS)
                .build();

        // 2. 使用Selenium提供的OkHttpClient工厂替代原有的ApacheHttpClient.Factory
        org.openqa.selenium.remote.internal.OkHttpClient.Factory clientFactory =
                new org.openqa.selenium.remote.internal.OkHttpClient.Factory(okHttpClient);

        // 3. 启动EdgeDriverService(这部分和你原来的代码一致)
        edgeDriverService = new EdgeDriverService.Builder()
                .usingDriverExecutable(edgeDriver)
                .usingAnyFreePort()
                .build();
        edgeDriverService.start();

        // 4. 构建HttpCommandExecutor并初始化RemoteWebDriver
        HttpCommandExecutor executor = new HttpCommandExecutor(
                new HashMap<>(), edgeDriverService.getUrl(), clientFactory);
        driver = new RemoteWebDriver(executor, new EdgeOptions());

        // 后续的测试逻辑...
    }
}

3. 依赖说明

如果使用Maven,确保你的pom.xml中包含以下依赖(版本至少为3.141.59):

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-remote-driver</artifactId>
    <version>3.141.59</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-edge-driver</artifactId>
    <version>3.141.59</version>
</dependency>

OkHttp的依赖会被自动引入,不需要单独添加。

额外说明

  • 如果你不需要自定义超时设置,可以直接使用默认的OkHttpClient.Factory,它会采用Selenium默认的超时配置:
    org.openqa.selenium.remote.internal.OkHttpClient.Factory clientFactory =
            new org.openqa.selenium.remote.internal.OkHttpClient.Factory();
    
  • 如果你坚持使用Selenium 3.14.0版本(不推荐),虽然ApacheHttpClient已被弃用,但仍然可以暂时使用,但建议尽快升级到稳定版本避免后续兼容性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 03:57:04