文章目錄
- 一、單點觸控
- 查看指針的指針位置
- 實現手勢密碼:
- 二、多點觸控
一、單點觸控
查看指針的指針位置
方便查看手勢密碼-九宮格每個點的坐標
實現手勢密碼:
- 執行手勢操作: 按壓起點 -> 移動到下一點 -> 依次移動 -> 釋放,最后調用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 sleepfrom 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_ystart_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()