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

Selenium初始化Firefox WebDriver失败,报NoSuchMethodError错误求助

Firefox WebDriver初始化报错排查求助

初始化Firefox WebDriver时触发以下错误,导致自动化测试无法执行,推测是依赖版本不兼容,但调整依赖版本、指定GeckoDriver路径均未解决问题,求排查原因及解决方案。

错误信息

java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.service.DriverCommandExecutor.(org.openqa.selenium.remote.service.DriverService, java.util.Map)'

at org.openqa.selenium.firefox.FirefoxDriver$FirefoxDriverCommandExecutor.(FirefoxDriver.java:340)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:99)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:95)
at Web.Utils.BrowserUtil.createBrowser(BrowserUtil.java:27)
at Web.Utils.BrowserUtil.createBrowser(BrowserUtil.java:16)
at Btube.Web.PageObjects.Shared.AllPages.(AllPages.java:30)
at Web.Hooks.DefaultHooks.setup(DefaultHooks.java:22)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:578)
at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:139)
at org.testng.internal.invokers.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:69)
at org.testng.internal.invokers.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:361)
at org.testng.internal.invokers.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:296)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:372)
at org.testng.SuiteRunner.run(SuiteRunner.java:330)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1256)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1176)
at org.testng.TestNG.runSuites(TestNG.java:1099)
at org.testng.TestNG.run(TestNG.java:1067)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)

相关代码(BrowserUtil.java)

package Web.Utils;

import Web.Utils.WebConfig;
import io.github.bonigarcia.wdm.managers.ChromeDriverManager;
import io.github.bonigarcia.wdm.managers.FirefoxDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
// Import the necessary libraries
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class BrowserUtil {
    public static WebDriver createBrowser(){
        return createBrowser(WebConfig.getDefaultBrowser());
    }
    public static WebDriver createBrowser(String browserType){
        WebDriver driver;
        switch (browserType) {
            case "chrome" -> {
                ChromeDriverManager.chromedriver().setup();
                driver = new ChromeDriver();
            }
            case "firefox" -> {
                FirefoxDriverManager.firefoxdriver().setup();
                driver = new FirefoxDriver();
            }
            default -> throw new RuntimeException("Unsupported browser");
        }
        driver.manage().window().maximize();
        return driver;
    }
}

Maven POM依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Test_Framework</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.3.2</version>
        </dependency>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>8.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.7.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.truth</groupId>
            <artifactId>truth</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>4.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>4.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>4.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>4.8.1</version>
        </dependency>
        <dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>31.1-jre</version></dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.3.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.3.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>7.11.1</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>7.11.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.20.0</version>
        </dependency>
    </dependencies>
</project>

已尝试的解决方式

  • 修改Selenium、WebDriverManager的版本
  • 手动指定GeckoDriver路径

问题原因及解决方案

原因分析

错误核心是依赖版本冲突:Appium Java Client 8.3.0默认依赖的Selenium版本低于项目指定的4.8.1,导致运行时加载了旧版本的org.openqa.selenium.remote.service.DriverCommandExecutor类,其构造方法签名与Selenium 4.8.1不匹配,触发NoSuchMethodError。

解决方案

  1. 排除Appium依赖中的Selenium包
    修改POM中Appium的依赖配置,排除所有Selenium相关子依赖,确保项目只使用指定的4.8.1版本:

    <dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>8.3.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-remote-driver</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-support</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
  2. 优化WebDriverManager的使用方式
    WebDriverManager 5.x版本推荐使用统一入口WebDriverManager类,而非单独的ChromeDriverManager/FirefoxDriverManager,修改BrowserUtil代码:

    package Web.Utils;
    
    import Web.Utils.WebConfig;
    import io.github.bonigarcia.wdm.WebDriverManager;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class BrowserUtil {
        public static WebDriver createBrowser(){
            return createBrowser(WebConfig.getDefaultBrowser());
        }
        public static WebDriver createBrowser(String browserType){
            WebDriver driver;
            switch (browserType) {
                case "chrome" -> {
                    WebDriverManager.chromedriver().setup();
                    driver = new ChromeDriver();
                }
                case "firefox" -> {
                    WebDriverManager.firefoxdriver().setup();
                    driver = new FirefoxDriver();
                }
                default -> throw new RuntimeException("Unsupported browser");
            }
            driver.manage().window().maximize();
            return driver;
        }
    }
    

    同时删除重复的导入语句,避免代码冗余。

  3. 清理Maven缓存并重新构建
    执行以下命令清理本地Maven缓存,确保依赖完全更新:

    mvn clean install -U
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 12:32:34