python+Appium自动化:TouchAction九宫格实战

TouchAction

Touch Action包含一系列操作,比如按压、长按、点击、移动、暂停。

使用TochAction需要先导入对应的模块

from appium.webdriver.common.touch_action import TouchAction

按压

使用到press()方法,通过手指按压手机屏幕的某个位置, press可以是一个元素,也可以接收屏幕的坐标(x,y)。

press(self, el=None, x=None, y=None)

TouchAction(driver).press(x=200,y=200).release().perform()

长按

使用到longpress()方法,与press相似,比press多了一个duration延迟时间(毫秒)

long_press(self, el=None, x=None, y=None, duration=1000)

TouchAction(driver).long_press(x=200,y=200,duration=1000).release().perform()

点击

使用到tap() 方法,可以对一个元素或控件执行点击操作,用法参考press()。

tap(self, element=None, x=None, y=None, count=1)

移动

使用到move_to() 方法,将指针从上一个点移动到指定的元素或点。

move_to(self, el=None, x=None, y=None)

注意:

移动到目位置有时是算绝对坐标点,有时是基于前面一个坐标点的偏移量,这个要结合具体App来实践。

暂停

方法:Wait()

wait(self, ms=0),暂停脚本的执行,单位为毫秒。

释放

方法release() 结束的行动取消屏幕上的指针。

release(self)

执行

perform() 执行的操作发送到服务器的命令操作。

perform(self)

TouchAction实战——九宫格滑动操作

案例场景:

进入手机自带的app手机管家,有应用已进行过加密,需要九宫格解锁才能进入设置其它应用加密权限。

 代码实现:

# -*- coding: utf-8 -*-#

from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
desired_caps = {
               "platformName": "Android",
               "platformVersion": "5.1",
               "deviceName": "U4KF9HSK99999999",
               "appPackage": "com.coloros.safecenter",
               "appActivity": "com.coloros.safecenter.MainActivity",
               "unicodeKeyboard":True,
               "resetKeyboard":True,
               "noReset": True
               #"ANDROID_UIAUTOMATOR":"Uiautomator2",
               # "chromeOptions": {"androidProcess": "com.wondershare.drfone"}
                }
driver = webdriver.Remote("http://localhost:4723/wd/hub",desired_caps)
driver.implicitly_wait(5)
driver.find_element_by_id("com.coloros.safecenter:id/image_permission").click()
time.sleep(2)
driver.find_element_by_xpath("//*[@text='XXXX']").click()
time.sleep(2)
#开始滑动解锁,此app就是根据坐标的偏移量计算的
TouchAction(driver).press(x=270,y=791).wait(2000).
    move_to(x=270,y=0).wait(1000).
    move_to(x=270,y=0).wait(1000).
    move_to(x=0,y=270).wait(1000).release().perform()


try:
    driver.find_element_by_class_name("android.widget.Switch")

except NoSuchElementException:
    print("解锁失败!")
else:
    print("解锁成功!")

参考转载:https://www.cnblogs.com/xuzhongtao/p/9723222.html

转载请附上原文链接。
原文地址:https://www.cnblogs.com/bugbreak/p/12068778.html