Appium 3.x 手势操作实战:clickGesture与dragGesture详解

📅 2026/7/24 22:43:27
Appium 3.x 手势操作实战:clickGesture与dragGesture详解
Appium 3.x 实战笔记mobile 手势标准写法 —— clickGesture / dragGesture 快速上手适配 Python Client 5.x前言Appium 3.x Appium-Python-Client 5.x 环境下手势操作统一使用mobile:命令通过driver.execute_script执行。clickGesture替代了tapdragGesture替代了drag_and_drop写法更加统一、跨语言通用。本文以系统设置 APP 为例演示坐标点击、元素拖拽的完整代码与参数解析所有步骤在雷电 9Android 9上验证通过。本文为个人原创学习笔记发布于 CSDN 仅作技术交流Appium 遵循 Apache 2.0 开源协议。一、环境准备importtimefromappiumimportwebdriverfromappium.options.commonimportAppiumOptionsfromappium.webdriver.common.appiumbyimportAppiumBy caps{platformName:Android,appium:platformVersion:9,appium:deviceName:25102RKBEC,appium:automationName:UiAutomator2,appium:appPackage:com.android.settings,appium:appActivity:com.android.settings.Settings,appium:noReset:True,}optionsAppiumOptions()options.load_capabilities(caps)driverwebdriver.Remote(http://127.0.0.1:4723,optionsoptions)driver.implicitly_wait(10)# 全局隐式等待 10 秒二、轻敲指定坐标mobile: clickGesture# 点击屏幕坐标 (194, 225)driver.execute_script(mobile: clickGesture,{x:194,y:225})time.sleep(2)# 等待页面响应参数传入x和y即屏幕绝对坐标像素。替代作用完全取代旧版driver.tap([(x, y)])更简洁、跨语言一致。三、返回上一页driver.find_element(AppiumBy.XPATH,//*[content-desc向上导航]).click()time.sleep(2)使用AppiumBy.XPATH定位返回按钮。content-desc属性在原生应用中稳定性极高推荐优先使用。四、拖拽元素到目标坐标mobile: dragGestureeldriver.find_element(AppiumBy.XPATH,//*[textWLAN、移动网络、流量使用])driver.execute_script(mobile: dragGesture,{elementId:el.id,# 拖拽源元素endX:1023,# 终点 X 坐标endY:368# 终点 Y 坐标})参数详解elementId必填被拖拽元素的 ID通过el.id获取。endX/endY拖拽终点的屏幕绝对坐标。可选startX、startY不传则从元素中心点开始。效果手指按住元素移动到终点后释放。五、关闭应用与结束会话time.sleep(5)# 观察拖拽结果driver.execute_script(mobile: terminateApp,{appId:com.android.settings})driver.quit()六、新版手势体系速查操作旧版已废弃新版mobile:命令点击坐标driver.tap([(x, y)])driver.execute_script(mobile: clickGesture, {x: x, y: y})拖拽元素到坐标无直接方法driver.execute_script(mobile: dragGesture, {elementId: id, endX: x, endY: y})拖拽元素到元素driver.drag_and_drop(el1, el2)driver.execute_script(mobile: dragGesture, {elementId: id1, elementId2: id2})(部分版本支持)七、注意事项坐标单位是像素不同设备需重新适配建议优先用elementId操作元素本身。elementId是 Appium 运行时的动态 ID不可硬编码必须通过find_element获取。隐式等待implicitly_wait仅对元素查找生效execute_script前应确保元素已存在可配合time.sleep或WebDriverWait。所有mobile:命令的参数均为 JSON 对象键名区分大小写请与官方文档保持一致。版权与参考说明本文为个人原创学习笔记所有代码与案例均为实操整理发布于 CSDN 仅作技术交流Appium 为开源自动化测试框架遵循 Apache 2.0 开源协议参考资料Appium Mobile Commands 文档。