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

如何将Appium的driver.swipe转换为TouchAction适配Eclipse运行?

我来帮你搞定这个问题!Appium 新版本已经弃用了driver.swipe()方法,所以你在Eclipse里会遇到无法识别的情况。下面我给你两种可行的替代方案,分别适配不同版本的Appium,你可以根据自己的环境选择:

方案1:使用TouchAction(适用于Appium 1.x版本)

如果你还在使用Appium 1.x,TouchAction是官方推荐的触摸操作替代方案,它可以模拟一系列连贯的触摸动作,包括滑动。

示例1:基于坐标的滑动(从屏幕上方向下方滑动)

先获取屏幕宽高,再计算滑动的起点和终点坐标,适配不同分辨率设备:

import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import java.time.Duration;

// 获取当前屏幕尺寸
int screenWidth = driver.manage().window().getSize().getWidth();
int screenHeight = driver.manage().window().getSize().getHeight();

// 定义滑动起点(屏幕顶部中间)和终点(屏幕底部中间)
PointOption startPoint = PointOption.point(screenWidth / 2, screenHeight * 1 / 4);
PointOption endPoint = PointOption.point(screenWidth / 2, screenHeight * 3 / 4);

// 执行滑动操作
new TouchAction(driver)
    .press(startPoint)
    .waitAction(WaitOptions.waitOptions(Duration.ofMillis(500))) // 模拟真实滑动的停顿
    .moveTo(endPoint)
    .release()
    .perform();

示例2:字段间的滑动(从元素A滑到元素B)

如果需要在两个指定字段之间滑动,先定位元素再获取它们的中心坐标:

import io.appium.java_client.MobileElement;
import org.openqa.selenium.By;

// 定位需要滑动的两个字段
MobileElement fieldA = (MobileElement) driver.findElement(By.id("com.your.app:id/field_a"));
MobileElement fieldB = (MobileElement) driver.findElement(By.id("com.your.app:id/field_b"));

// 获取元素的中心坐标
PointOption startPoint = PointOption.point(fieldA.getCenter().x, fieldA.getCenter().y);
PointOption endPoint = PointOption.point(fieldB.getCenter().x, fieldB.getCenter().y);

// 执行滑动
new TouchAction(driver)
    .press(startPoint)
    .waitAction(WaitOptions.waitOptions(Duration.ofMillis(300)))
    .moveTo(endPoint)
    .release()
    .perform();
方案2:使用W3C Actions(推荐,适用于Appium 2.x及更高版本)

Appium 2.x开始推荐使用W3C标准的Actions类,它是Selenium 4+的官方触摸操作API,兼容性和稳定性更好。

示例1:基于坐标的滑动

import org.openqa.selenium.Point;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import java.time.Duration;
import java.util.Collections;

// 定义触摸输入源(模拟手指)
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
int screenWidth = driver.manage().window().getSize().getWidth();
int screenHeight = driver.manage().window().getSize().getHeight();

// 创建滑动动作序列:按下 → 移动 → 释放
Sequence swipe = new Sequence(finger, 1)
    .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), screenWidth / 2, screenHeight * 1 / 4))
    .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
    .addAction(finger.createPointerMove(Duration.ofMillis(500), PointerInput.Origin.viewport(), screenWidth / 2, screenHeight * 3 / 4))
    .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

// 执行滑动
driver.perform(Collections.singletonList(swipe));

示例2:字段间的滑动

import io.appium.java_client.MobileElement;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import java.time.Duration;
import java.util.Collections;

// 定位字段并获取中心坐标
MobileElement fieldA = (MobileElement) driver.findElement(By.id("com.your.app:id/field_a"));
MobileElement fieldB = (MobileElement) driver.findElement(By.id("com.your.app:id/field_b"));

Point start = fieldA.getCenter();
Point end = fieldB.getCenter();

PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence swipe = new Sequence(finger, 1)
    .addAction(finger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), start.x, start.y))
    .addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
    .addAction(finger.createPointerMove(Duration.ofMillis(300), PointerInput.Origin.viewport(), end.x, end.y))
    .addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

driver.perform(Collections.singletonList(swipe));
几个关键提示
  • 依赖包匹配:确保你的Java Client版本和Appium版本对应,比如Appium 2.x需要搭配Selenium 4+的Java Client包。
  • 元素等待:滑动前一定要用WebDriverWait等待目标元素完全可见,避免出现元素未找到的异常。
  • 设备适配:优先使用元素的相对位置而非固定坐标,这样测试用例能适配不同分辨率的Android设备。

内容的提问来源于stack exchange,提问作者LUCAS M SANTOS

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:46:46