Appium 手势操作双雄对决W3C Actions 与 Mobile Commands 完全指南 命令对照表小白向前言刚接触 Appium 自动化的新手最头疼的就是“怎么让手机屏幕动起来”——点击、长按、滑动、拖拽、缩放……其实在 Appium 3.x Python Client 5.x 时代官方给了两套手势方案一套是符合W3C 国际标准的Actions另一套是 Appium 自家的mobile:扩展命令。本文抛开复杂的理论用最直白的代码模板把这两套方案掰开揉碎讲清楚。结尾附上两大体系的功能对比表格 每个手势的详细命令对照表并告诉你新手应该先学哪一套。所有代码均可在雷电 9Android 9 系统设置 APP 下直接运行。本文为个人原创学习笔记发布于 CSDN 仅作技术交流Appium 遵循 Apache 2.0 开源协议。第一部分正规军 —— W3C Actions推荐必学这是 Appium 2.0 就开始推广的标准写法逻辑非常清晰手指按下 → 移动 → 抬起。代码虽然长一点但每一步都看得见适合需要精细控制或面试时展示原理的场景。环境准备必须导入的库fromselenium.webdriver.common.action_chainsimportActionChainsfromselenium.webdriver.common.actionsimportinteractionfromselenium.webdriver.common.actions.action_builderimportActionBuilderfromselenium.webdriver.common.actions.pointer_inputimportPointerInput1. 点击坐标# 定义一个“手指”触摸输入fingerPointerInput(interaction.POINTER_TOUCH,touch)# 创建动作链actionsActionBuilder(driver,mousefinger,keyboardNone)# 动作移动到坐标 (500, 500) → 按下 → 释放actions.pointer_action.move_to_location(500,500)actions.pointer_action.pointer_down()# 手指按下actions.pointer_action.pointer_up()# 手指抬起# 执行actions.perform()2. 长按actionsActionBuilder(driver,mousePointerInput(interaction.POINTER_TOUCH,touch),keyboardNone)actions.pointer_action.move_to_location(500,500)actions.pointer_action.pointer_down()actions.pointer_action.pause(2)# 按住不动 2 秒单位秒actions.pointer_action.pointer_up()actions.perform()3. 滑动从 A 点滑到 B 点start_x,start_y500,1500end_x,end_y500,500actionsActionBuilder(driver,mousePointerInput(interaction.POINTER_TOUCH,touch),keyboardNone)actions.pointer_action.move_to_location(start_x,start_y)actions.pointer_action.pointer_down()actions.pointer_action.move_to_location(end_x,end_y)# 移动到终点actions.pointer_action.pointer_up()actions.perform()4. 拖拽抓取元素 A 拖到坐标 Bstart_x,start_y100,100# 元素 A 的坐标end_x,end_y300,300# 目标坐标actionsActionBuilder(driver,mousePointerInput(interaction.POINTER_TOUCH,touch),keyboardNone)actions.pointer_action.move_to_location(start_x,start_y)actions.pointer_action.pointer_down()actions.pointer_action.pause(0.5)# 稍停确保“抓住”actions.pointer_action.move_to_location(end_x,end_y)actions.pointer_action.pause(0.5)actions.pointer_action.pointer_up()actions.perform()要点总结W3C Actions 就像写剧本手指的每一步动作移动、按下、暂停、抬起都要明确指令非常适合复杂连续手势。第二部分捷径版 —— Mobile Commands偷懒专用这套命令是 Appium 通过execute_script提供的“魔法咒语”代码极短功能强大尤其适合滑动、缩放等操作。这是目前企业升级到 3.x 后的主流写法。1. 点击# 方式一点击坐标driver.execute_script(mobile: clickGesture,{x:500,y:500})# 方式二点击元素推荐更稳定eldriver.find_element(AppiumBy.XPATH,//*[text电池])driver.execute_script(mobile: clickGesture,{elementId:el.id})2. 双击eldriver.find_element(AppiumBy.XPATH,//*[text电池])driver.execute_script(mobile: doubleClickGesture,{elementId:el.id})3. 长按driver.execute_script(mobile: longClickGesture,{elementId:el.id,duration:2000# 按住 2000 毫秒 2 秒})4. 滑动方向滑动最好用不需要算终点坐标直接告诉方向driver.execute_script(mobile: swipeGesture,{left:100,# 起点 X 坐标top:1000,# 起点 Y 坐标width:200,# 滑动区域宽度height:200,# 滑动区域高度direction:up,# 方向up / down / left / rightpercent:0.75# 滑动距离占宽/高的比例0.0~1.0})举例从屏幕底部向上滑 50% 的距离direction: up, percent: 0.5。也可以用精确坐标driver.execute_script(mobile: swipeGesture,{startX:500,startY:1200,endX:500,endY:400})5. 拖拽source_eldriver.find_element(AppiumBy.XPATH,//*[textWLAN、移动网络、流量使用])driver.execute_script(mobile: dragGesture,{elementId:source_el.id,endX:1023,endY:368})6. 双指缩放# 放大两指向外扩张driver.execute_script(mobile: pinchOpenGesture,{elementId:map_el.id,percent:0.5# 扩张程度})# 缩小两指向内捏合driver.execute_script(mobile: pinchCloseGesture,{elementId:map_el.id,percent:0.5})要点总结Mobile Commands 把常用手势高度封装一句代码搞定非常适合快速开发。第三部分双雄对决 —— 两版对比表格1. 功能维度对比对比维度W3C ActionsMobile Commands (mobile:)代码量较长需逐步构建“按下-移动-抬起”极短一个execute_script完成上手难度略高需要理解 ActionBuilder 机制低参数名直观照着模板改就行跨语言一致性强Selenium/Appium 各语言通用强JSON 参数各语言一致支持的手势所有单点、多点多指触控覆盖 90% 常用手势精确控制高可控制每一步的持续时间、坐标一般参数固定无法自定义暂停缩放、旋转等需自己组合多个指针较复杂一行代码搞定极爽维护成本代码多修改稍微麻烦代码少修改快适用场景面试展示、极复杂手势、多指同时操作日常脚本、快速开发、维护量大的项目2. 详细命令对照表手势操作W3C Actions 写法Mobile Commands 写法点击坐标actions.move_to_location(x,y).pointer_down().pointer_up()execute_script(mobile: clickGesture, {x: x, y: y})点击元素需先获取元素坐标再点击或使用click()execute_script(mobile: clickGesture, {elementId: el.id})长按坐标actions.move_to_location(x,y).pointer_down().pause(t).pointer_up()execute_script(mobile: longClickGesture, {x: x, y: y, duration: ms})长按元素同上需坐标execute_script(mobile: longClickGesture, {elementId: el.id, duration: ms})双击需组合两次点击 短暂间隔execute_script(mobile: doubleClickGesture, {elementId: el.id})滑动方向手动计算起止坐标构建动作链execute_script(mobile: swipeGesture, {direction: up, percent: 0.5})滑动精确坐标actions.move_to(x1,y1).pointer_down().move_to(x2,y2).pointer_up()execute_script(mobile: swipeGesture, {startX: x1, startY: y1, endX: x2, endY: y2})拖拽元素到坐标需手动获取元素坐标构建长按移动execute_script(mobile: dragGesture, {elementId: el.id, endX: x, endY: y})拖拽元素到元素需分别获取两元素坐标execute_script(mobile: dragGesture, {elementId: el1.id, elementId2: el2.id})(部分版本支持)双指放大需同时管理两根手指的移动轨迹execute_script(mobile: pinchOpenGesture, {elementId: el.id, percent: 0.5})双指缩小同上execute_script(mobile: pinchCloseGesture, {elementId: el.id, percent: 0.5})四、给小白的最优学习路线先玩转mobile:命令把swipeGesture、clickGesture、dragGesture跑通快速获得正反馈写脚本更有信心。再了解 W3C Actions 的基本结构不要求全记住但要知道“手指按下→移动→抬起”这个核心逻辑面试时能说清楚。实际工作中优先使用mobile:命令企业项目追求效率与可维护性mobile:命令是目前的主流实践。版权与参考说明本文为个人原创学习笔记所有代码与案例均为实操整理发布于 CSDN 仅作技术交流Appium 为开源自动化测试框架遵循 Apache 2.0 开源协议参考资料Appium Mobile Commands 文档。