分析九宫格定位
整个九宫格是一个 view
self.driver.find_element_by_id("com.elc:id/gesturepwd_create_lockview")
屏幕大小 x 720, y 1280
九宫格定位 x 80, y 488, width 640 height 1048
计算出大小 x 80 y 488
width = 640-80 width 等于 560
height = 1048-488 height 等于 560
九宫格是正方形
计算出第一个坐标 为了好计算,就把坐标的值全部用整数来计算
p11 = int(x + width / 6), int(y + height / 6)
(173, 581)
计算所有的坐标值
定位方式存在一个字典中
a = {
"解锁提示": "com.elc:id/gesturepwd_create_text",
"九宫格": "com.elc:id/gesturepwd_create_lockview",
"继续": "com.elc:id/right_btn",
}
lock_pattern = self.driver.find_element_by_id(a["九宫格"])
x = lock_pattern.location.get('x')
y = lock_pattern.location.get('y')
width = lock_pattern.size.get('width')
height = lock_pattern.size.get('height')
print(x, y, width, height)
offset = width / 6
p11 = int(x + width / 6), int(y + height / 6)
p12 = int(x + width / 2), int(y + height / 6)
p13 = int(x + width - offset), int(y + height / 6)
p21 = int(x + width / 6), int(y + height / 2)
p22 = int(x + width / 2), int(y + height / 2)
p23 = int(x + width - offset), int(y + height / 2)
p31 = int(x + width / 6), int(y + height - offset)
p32 = int(x + width / 2), int(y + height - offset)
p33 = int(x + width - offset), int(y + height - offset)
print(p11, p12, p13)
print(p21, p22, p23)
print(p31, p32, p33)
80 488 560 560 # 九宫格坐标值 与上面计算的一样
(173, 581) (360, 581) (546, 581)
(173, 768) (360, 768) (546, 768)
(173, 954) (360, 954) (546, 954)
计算滑动偏移量(做的效果图是 7)
p3 = p13[0] - p11[0] # 偏移的值是 373
滑动方法 TouchAction
TouchAction(self.driver).press(x=p11[0], y=p11[1]).move_to(x=p3, y=0).wait(1000).move_to(x=0, y=p3).wait(
1000).release().perform()
说明 滑动方法
# 横向滑动
从 173 滑动到 546 坐标的偏移量是 546-173 = 373
第一个值不变 press(x=p11[0], y=p11[1]).move_to(x=373, y =0),因为y 的值是相同的,就不变
# 纵向滑动
move_to(x=0, y=373) 因为x的值不变,移动y 的值
九宫格是正方的形,所以 从第1点到3点是 373, 从3点到9点也是 373
九宫格解锁完毕
这样的写法,不需要固定坐标值,根据不同的设备屏幕大小自动获取到--偏移值
我遇到的坑-- 之前一直以为 TouchAction 的 move_to 是从x y 坐标移动到另一个坐标,而不是偏移量,没有理解清楚,所以这里掉坑里了
最后附上完整的函数
def login_unlock(self):
a = {
"解锁提示": "com.elc:id/gesturepwd_create_text",
"九宫格": "com.elc:id/gesturepwd_create_lockview",
"继续": "com.elc:id/right_btn",
}
unlock_text = self.driver.find_element_by_id(a["解锁提示"])
lock_pattern = self.driver.find_element_by_id(a["九宫格"])
x = lock_pattern.location.get('x')
y = lock_pattern.location.get('y')
width = lock_pattern.size.get('width')
height = lock_pattern.size.get('height')
print(unlock_text.text)
print(x, y, width, height)
offset = width / 6
p11 = int(x + width / 6), int(y + height / 6)
p12 = int(x + width / 2), int(y + height / 6)
p13 = int(x + width - offset), int(y + height / 6)
p21 = int(x + width / 6), int(y + height / 2)
p22 = int(x + width / 2), int(y + height / 2)
p23 = int(x + width - offset), int(y + height / 2)
p31 = int(x + width / 6), int(y + height - offset)
p32 = int(x + width / 2), int(y + height - offset)
p33 = int(x + width - offset), int(y + height - offset)
p3 = p13[0] - p11[0]
sleep(3)
TouchAction(self.driver).press(x=p11[0], y=p11[1]).move_to(x=p3, y=0).wait(1000).move_to(x=0, y=p3).wait(
1000).release().perform()
效果视频
Vimeo
https://v.youku.com/v_show/id_XMjk4NjY0MjQwMA==.html?spm=a2hzp.8244740.0.0