1.TouchAction用法
TouchAction下面提供的方法:
按压:press()
移动:move_to()
暂停:wait() 等待时间,单位毫秒
释放:release() 结束屏幕上的一系列动作的命令操作
执行:perform() 讲执行的操作发送到服务器的命令操作
2.举例:案例1
(1)分析
现在很多APP登录都会支持手势密码登录,所以手势密码登录怎么做呢?这里会使用appium.webdriver.common.touch_action下面封装的一个类TouchAction中的按压、移动、等待、释放、执行方法。这里的每个点可以通过class_name+index来定位到每个点的index依次为0,1,2,3,4,5,6,7,8,9;也可以直接通过class_name获取一个点的集合,这里点的集合是从list中的第二个开始
(2)代码实现
#这里准备绘制的路径为(index)0-3-6-7-8
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
list_pwd = driver.find_elements_by_class_name("android.view.View")
#第一次绘制手势密码
TouchAction(driver).press(list_pwd[1]).move_to(list_pwd[1]).move_to(list_pwd[4]).wait(100).move_to(
list_pwd[7]).wait(100).move_to(list_pwd[8]).wait(100).move_to(list_pwd[9]).release().perform()
#第二次绘制手势密码
try:
loc = ("xpath", "//*[@text='请再次绘制解锁图案']")
e = WebDriverWait(driver, 1, 0.5).until(EC.presence_of_element_located(loc))
TouchAction(driver).press(list_pwd[1]).move_to(list_pwd[1]).move_to(list_pwd[4]).wait(100).move_to(
list_pwd[7]).wait(100).move_to(list_pwd[8]).wait(100).move_to(list_pwd[9]).release().perform()
except Exception as e:
print("[Error]: ", e)
3.举例:案例2
(1)支付宝的手势密码,不同于案例1,它是整个手势密码一个元素,没有每个点一个元素
(2)代码实现
def point_nine( ele):
"""
:param ele: 手势图元素
:return: 手势密码9点坐标
"""
# 获取元素坐标
ele_location = ele.location
x = ele_location.get('x')
y = ele_location.get('y')
# 获取元素宽、高
ele_size = ele.size
width = ele_size['width']
height = ele_size['height']
xstep = int(width / 6)
ystep = int(height / 6)
beginx = int(x + xstep)
beginy = int(y + ystep)
list_point = {
"1": [beginx, beginy],
"2": [beginx + 2 * xstep, beginy],
"3": [beginx + 4 * xstep, beginy],
"4": [beginx, beginy + 2 * ystep],
"5": [beginx + 2 * xstep, beginy + 2 * ystep],
"6": [beginx + 4 * xstep, beginy + 2 * ystep],
"7": [beginx, beginy + 4 * ystep],
"8": [beginx + 2 * xstep, beginy + 4 * ystep],
"9": [beginx + 4 * xstep, beginy + 4 * ystep]
}
return list_point
def gesture_code( driver, ele, password_list):
# 调用point_nine()获取9个点坐标
points = point_nine(ele)
t = TouchAction(driver)
# 获取第一个点坐标进行按压
point_one = password_list[0]
t.press(x=points[point_one][0], y=points[point_one][1])
for i in range(1, len(password_list)):
i = password_list[i]
t.move_to(x=points[i][0], y=points[i][1])
t.wait(200)
t.release().perform().wait(500)
#主函数中
# password_list是你的手势密码对应的点
# 手势图
lockView = ['id', "com.alipay.mobile.gesturebiz:id/lockView"]
password_list = ['1', '4', '7', '8', '9']
lockView_ele = driver.find_element(*lockView)
gesture_code(driver, lockView_ele, password_list)