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

使用Kotlin时Selenium调用CdpEndpointFinder出现方法缺失错误

解决Selenium中NoSuchMethodError: CdpEndpointFinder.getHttpClient的问题

你遇到的NoSuchMethodError完全是Selenium各组件版本不兼容导致的。当前依赖配置里,selenium-java(4.8.1)、selenium-devtools(4.0.0-rc-1)、selenium-chrome-driver(4.14.1)版本差异过大,不同版本的CDP(Chrome DevTools Protocol)相关类结构已发生变更,低版本的selenium-devtools中不存在高版本chrome-driver调用的getHttpClient方法。

解决方案

  1. 统一所有Selenium组件版本
    将所有Selenium相关依赖的版本保持一致,建议与你当前使用的selenium-chrome-driver版本(4.14.1)对齐,修改后的settings.gradle.kts如下:

    plugins {
        kotlin("jvm") version "1.9.0"
        application
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testImplementation(kotlin("test"))
        // 统一Selenium组件版本为4.14.1
        implementation("org.seleniumhq.selenium:selenium-java:4.14.1")
        implementation("org.seleniumhq.selenium:selenium-devtools:4.14.1")
        implementation("org.seleniumhq.selenium:selenium-chrome-driver:4.14.1")
    
        // log4j依赖保持不变
        implementation("org.apache.logging.log4j:log4j-core:2.21.0")
        implementation("org.apache.logging.log4j:log4j-api:2.21.0")
        testImplementation("org.apache.logging.log4j:log4j-slf4j2-impl:2.21.0")
    }
    
  2. 简化测试代码(可选但推荐)
    Selenium 4.x版本后,无需手动设置webdriver.chrome.driver路径,Selenium Manager会自动匹配并下载与Chrome浏览器版本对应的驱动,简化后的代码如下:

    val options = ChromeOptions()
    options.addArguments("--remote-allow-origins=*")
    val driver = ChromeDriver(options)
    
    driver.get("https://www.garfieldtech.com/ip.php")
    

补充说明

  • 确保你的Chrome浏览器版本与ChromeDriver版本匹配(如Chrome 118对应ChromeDriver 118.x),使用Selenium Manager可自动完成这一步。
  • 构建工具未给出兼容性警告是因为Gradle/Maven仅检查依赖是否存在,不会主动校验跨组件的API兼容性,这类运行时错误只能通过统一组件版本来避免。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 18:46:12