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

添加Selenium依赖后Appium Java测试报NoSuchMethodError求助

解决步骤

1. 移除不必要的内部依赖

你添加的org.openqa.selenium.interactions.internal和org.openqa.selenium.interactions.touch是Selenium的私有内部包,Selenium 4.x版本已将这些功能整合到核心依赖中,单独引入会破坏依赖树,引发版本冲突。直接在pom.xml中删除这两个依赖项。

2. 替换旧的触摸操作代码

Selenium 4.1.3已重构触摸API,旧的touch包相关类(如TouchActions)已被弃用或移除,改用以下方式实现触摸操作:

  • 若使用Appium,直接用Appium提供的TouchAction类(需确保Appium Java Client版本与Selenium 4.1.3兼容,建议用8.x版本):
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.offset.PointOption;

// 示例:点击操作
new TouchAction(driver)
    .tap(PointOption.point(100, 200))
    .perform();
  • 若用Selenium原生API,使用Actions类的触摸方法:
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import java.time.Duration;

// 示例:点击操作
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence tap = new Sequence(finger, 1)
        .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), 100, 200))
        .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
        .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
new Actions(driver).perform(tap);

3. 锁定Guava版本解决方法缺失问题

报错根源是依赖冲突导致Guava版本过低,Selenium 4.1.3依赖Guava 31.0.1-jre,在pom.xml的dependencyManagement中添加版本锁定:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.0.1-jre</version>
        </dependency>
    </dependencies>
</dependencyManagement>

4. 刷新Maven依赖

执行以下命令清理并重新安装依赖,确保冲突依赖被排除:

mvn clean install -U

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 12:35:17