轉發:https://testerhome.com/topics/9698
使用Python來解決
分析九宮格定位
整個九宮格是一個 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()