05 APP 自动化- Appium 单点触控& 多点触控

文章目录

      • 一、单点触控
          • 查看指针的指针位置
          • 实现手势密码:
      • 二、多点触控

一、单点触控

查看指针的指针位置

方便查看手势密码-九宫格每个点的坐标

实现手势密码:
  • 执行手势操作: 按压起点 -> 移动到下一点 -> 依次移动 -> 释放,最后调用perform()

  • 实现的效果如下图:

  • 代码实现:

    -- coding=utf-8 --

    from appium import webdriver
    from appium.options.android import UiAutomator2Options
    from appium.webdriver.common.appiumby import AppiumBy

    旧方法(TouchAction,已弃用)

    新方法(W3C Actions API)

    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.actions import interaction
    from selenium.webdriver.common.actions.action_builder import ActionBuilder
    from selenium.webdriver.common.actions.pointer_input import PointerInput

    设置操作终端的配置参数

    desired_caps = dict(
    platformName='Android', # 指定操作系统
    platformVersion='12',# 指定操作系统版本
    automationName='Uiautomator2',# 默认框架
    deviceName='127.0.0.1:62001',# 指定设备名称
    appPackage='com.mymoney',# 被操作的应用程序包名
    appActivity='com.mymoney.biz.splash.SplashScreenActivity',# 启动页面
    noReset='true',# true--不重置 false--重置
    app='F:\Pycharm\AppAuto\Mymoney_v13.2.18.apk' # apk文件所在路径
    )

    发送命令给 appium server

    driver = webdriver.Remote('http://127.0.0.1:4723', options=UiAutomator2Options().load_capabilities(desired_caps))

    打开随手记app->进入我的页面->点击设置一栏->点击密码保护一栏->开启密码保护->点击手势密码->进入手势密码设置页面

    1. 初始化 ActionChains 和触摸输入源

    actions = ActionChains(driver)
    actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))

    2. 定义手势轨迹的坐标点(假设已确定坐标,或从 bounds 解析计算 )

    示例坐标,需根据实际应用调整

    point_1 = (345, 384)
    point_2 = (540, 384)
    point_3 = (740, 384)
    point_5 = (540, 580)
    point_7 = (345, 780)
    point_8 = (540, 780)
    point_9 = (740, 780)

    3. 执行手势操作:按压起点 -> 移动到下一点 -> 依次移动 -> 释放

    actions.w3c_actions.pointer_action.move_to_location(*point_1) # 移动到起点(不按压时,可先移动调整位置)
    actions.w3c_actions.pointer_action.pointer_down() # 按压起点
    actions.w3c_actions.pointer_action.pause(0.5) # 暂停 0.5 秒,模拟真实触摸停顿

    actions.w3c_actions.pointer_action.move_to_location(*point_2) # 移动到点 2
    actions.w3c_actions.pointer_action.pause(0.5)

    actions.w3c_actions.pointer_action.move_to_location(*point_3) # 移动到点 3
    actions.w3c_actions.pointer_action.pause(0.5)

    actions.w3c_actions.pointer_action.move_to_location(*point_5) # 移动到点 5
    actions.w3c_actions.pointer_action.pause(0.5)

    actions.w3c_actions.pointer_action.move_to_location(*point_7) # 移动到点 7
    actions.w3c_actions.pointer_action.pause(0.5)

    actions.w3c_actions.pointer_action.move_to_location(*point_8) # 移动到点 8
    actions.w3c_actions.pointer_action.pause(0.5)

    actions.w3c_actions.pointer_action.move_to_location(*point_9) # 移动到点 9
    actions.w3c_actions.pointer_action.pause(0.5)

    actions.w3c_actions.pointer_action.release() # 释放触摸

    4. 执行动作链

    actions.perform()

二、多点触控

以放大缩小图片为例:

复制代码
# -*- coding=utf-8 -*-
from time import sleep

from appium import webdriver
from appium.options.android import UiAutomator2Options
# 旧方法(TouchAction,已弃用)
# 新方法(W3C Actions API)
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from appium.webdriver.common.appiumby import AppiumBy

# 设置操作终端的配置参数
desired_caps = dict(
    platformName='Android', # 指定操作系统
    platformVersion='12',# 指定操作系统版本
    automationName='Uiautomator2',# 默认框架
    deviceName='127.0.0.1:62001',# 指定设备名称
    appPackage='com.android.browser',# 被操作的应用程序包名
    noReset='true',# true--不重置  false--重置
)
# 发送命令给 appium server
driver = webdriver.Remote('http://127.0.0.1:4723', options=UiAutomator2Options().load_capabilities(desired_caps))
# 打开系统自带浏览器->进入百度搜索页面->任意搜索一张图片->进入图片页面
# 定位到需要缩放的元素(如图片)
element = driver.find_element(AppiumBy.ID,"com.android.browser:id/main_content")
element_location = element.location  # 获取元素位置
element_size = element.size          # 获取元素尺寸

# 计算缩放中心点
center_x = element_location['x'] + element_size['width'] / 2
center_y = element_location['y'] + element_size['height'] / 2

# 定义双指初始位置和目标位置(根据缩放方向调整)
# 示例:双指从中心向两侧张开(放大)
start_x1 = center_x - 50  # 左手指起始位置
start_y1 = center_y
end_x1 = center_x - 150   # 左手指终点位置
end_y1 = center_y

start_x2 = center_x + 50  # 右手指起始位置
start_y2 = center_y
end_x2 = center_x + 150   # 右手指终点位置
end_y2 = center_y

# 创建 ActionBuilder 并添加两个触摸输入源(手指)
actions = ActionBuilder(driver)

#---------------------放大图片---------------------------
# 第一根手指(左手)的操作
finger1 = actions.add_pointer_input(interaction.POINTER_TOUCH, "finger1")
finger1.create_pointer_move(duration=0, x=start_x1, y=start_y1)  # 移动到起始位置
finger1.create_pointer_down()  # 按下
finger1.create_pointer_move(duration=500, x=end_x1, y=end_y1)  # 移动到终点(模拟张开)
finger1.create_pointer_up(button=0)    # 抬起

# 第二根手指(右手)的操作(与第一根手指同时进行)
finger2 = actions.add_pointer_input(interaction.POINTER_TOUCH, "finger2")
finger2.create_pointer_move(duration=0, x=start_x2, y=start_y2)  # 移动到起始位置
finger2.create_pointer_down()  # 按下(与第一根手指同步)
finger2.create_pointer_move(duration=500, x=end_x2, y=end_y2)  # 移动到终点(模拟张开)
finger2.create_pointer_up(button=0)    # 抬起

# 执行多点触控操作
actions.perform()

sleep(3)

#---------------------缩小图片---------------------------
# 第一根手指(左手)的操作
finger3 = actions.add_pointer_input(interaction.POINTER_TOUCH, "finger3")
finger3.create_pointer_move(duration=0, x=end_x1, y=end_y1)  # 移动到起始位置
finger3.create_pointer_down()  # 按下
finger3.create_pointer_move(duration=500, x=start_x1, y=start_y1)  # 移动到终点(模拟张开)
finger3.create_pointer_up(button=0)    # 抬起

# 第二根手指(右手)的操作(与第一根手指同时进行)
finger4 = actions.add_pointer_input(interaction.POINTER_TOUCH, "finger4")
finger4.create_pointer_move(duration=0, x=end_x2, y=end_y2)  # 移动到起始位置
finger4.create_pointer_down()  # 按下(与第一根手指同步)
finger4.create_pointer_move(duration=500, x=start_x2, y=start_y2)  # 移动到终点(模拟张开)
finger4.create_pointer_up(button=0)    # 抬起

# 执行多点触控操作
actions.perform()
相关推荐
清水白石00829 分钟前
从打印对象到高质量调试:彻底理解 Python 中 `__repr__` 和 `__str__` 的区别
开发语言·python
Sammyyyyy41 分钟前
Google I/O 2026 Antigravity 更新解析与 SDK 实战指南
python·ai编程·servbay
嫂子的姐夫1 小时前
047-MD5:飞卢网
爬虫·python·js逆向·逆向
DXM05211 小时前
第8期| 传统机器学习遥感解译:SVM & 随机森林分类全流程实操
人工智能·python·随机森林·机器学习·支持向量机·arcgis·自然语言处理
装不满的克莱因瓶1 小时前
深入PyTorch模型的训练与可视化 —— 掌握迁移学习等模型训练效果提升的办法
人工智能·pytorch·python·深度学习·神经网络·ai·迁移学习
无心水1 小时前
【OpenClaw:赚钱】案例19、内容产量5倍、广告收入翻4倍:播客转多平台内容矩阵全自动化实战(OpenAI Whisper + Claude)
java·人工智能·python·ai编程·openclaw·养龙虾·java.time
逗逗班学Python1 小时前
基于 Faster-Whisper 的本地语音转字幕与会议纪要系统:从音频转写到 SRT 字幕与 Markdown 纪要完整项目实战
python·语音识别·faster-whisper·字幕生成·会议纪要
The moon forgets1 小时前
ABot-M0:基于动作流形学习的机器人操作VLA基础模型深度解析
人工智能·pytorch·python·学习·具身智能·vla·点云分割
天空属于哈夫克32 小时前
企微 RPA 接口开放:无需官方权限,外部群自由操作
自动化·企业微信·api
Cloud_Shy6182 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第四章 Item 27 - 29)
开发语言·人工智能·经验分享·python·学习方法