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

Appium 2.x中“press”命令的替代方案及元素长按实现方法

Appium 2.x 实现长按操作的方案

在Appium 2.x中,TouchAction和MultiAction已被完全弃用,官方推荐使用W3C Actions API(通过ActionBuilder类实现)来完成触摸操作,包括长按。以下是主流语言的具体实现方式:

Python 示例

from appium.webdriver.common.action_chains import ActionChains

# 定位需要长按的元素
target_element = driver.find_element(By.ID, "your-element-id")

# 执行长按:按压元素 → 暂停3秒 → 释放
ActionChains(driver).press(target_element).pause(3).release().perform()

如果需要更底层的控制,也可以直接使用ActionBuilder:

from appium.webdriver.common.actions import interaction
from appium.webdriver.common.actions.action_builder import ActionBuilder
from appium.webdriver.common.actions.pointer_input import PointerInput

target_element = driver.find_element(By.ID, "your-element-id")

# 创建触摸动作构建器
actions = ActionBuilder(driver, mouse=PointerInput(interaction.TOUCH, "touch"))
actions.pointer_action.move_to_element(target_element)
actions.pointer_action.press()
actions.pointer_action.wait(3000)  # 等待时长,单位毫秒
actions.pointer_action.release()

actions.perform()

Java 示例

import org.openqa.selenium.interactions.Actions;
import java.time.Duration;

// 定位目标元素
WebElement targetElement = driver.findElement(By.id("your-element-id"));

// 执行长按操作
new Actions(driver)
        .press(targetElement)
        .pause(Duration.ofSeconds(3))
        .release()
        .perform();

底层序列实现方式:

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

WebElement targetElement = driver.findElement(By.id("your-element-id"));
PointerInput touch = new PointerInput(PointerInput.Kind.TOUCH, "touch");

Sequence sequence = new Sequence(touch, 1)
        .addAction(touch.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), targetElement.getLocation().getX(), targetElement.getLocation().getY()))
        .addAction(touch.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
        .addAction(new org.openqa.selenium.interactions.Pause(touch, Duration.ofSeconds(3)))
        .addAction(touch.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

driver.perform(Collections.singletonList(sequence));

JavaScript (WebDriverIO) 示例

// 定位元素
const targetElement = await $('~your-element-accessibility-id');

// 执行长按操作
await driver.performActions([
    {
        type: 'pointer',
        id: 'finger1',
        parameters: { pointerType: 'touch' },
        actions: [
            { type: 'pointerMove', duration: 0, origin: targetElement },
            { type: 'pointerDown', button: 0 },
            { type: 'pause', duration: 3000 }, // 3秒等待时长
            { type: 'pointerUp', button: 0 }
        ]
    }
]);

关键说明

  • 核心逻辑统一为按压目标元素 → 保持指定时长 → 释放,时长可根据需求调整(单位为毫秒或秒)。
  • 确保你的Appium客户端版本与Appium Server 2.x兼容,避免API不匹配问题。

内容的提问来源于stack exchange,提问作者Abdul Hossein Charkhi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 08:34:52