appium2 問題

nieda726發表於2024-12-30

def bezier_curve(sx, sy, ex, ey, cx, cy, part):
pointArr = []
changeX1 = (cx - sx) / part
changeY1 = (cy - sy) / part
# 控制點到結束點的 x 和 y 每次的增量
changeX2 = (ex - cx) / part
changeY2 = (ey - cy) / part
for i in range(part): # 計算兩個動點的座標
qx1 = sx + changeX1 * i
qy1 = sy + changeY1 * i
qx2 = cx + changeX2 * i
qy2 = cy + changeY2 * i
# 計算得到此時的一個貝塞爾曲線上的點座標
bx = qx1 + (qx2 - qx1) * i / part
by = qy1 + (qy2 - qy1) * i / part
pointArr.append([bx, by])
return pointArr

獲取螢幕尺寸

window_size = driver.get_window_size()
screen_width = window_size['width']
screen_height = window_size['height']

定義曲線滑動的座標點列表,可按需增加更多點來細化曲線

point_list = bezier_curve(100,100, 100, 600, 200, 300, 10)

建立 ActionChains 物件

action_chains = ActionChains(driver)

移動到起始點並按下

action_chains.w3c_actions.pointer_action.move_to_location(point_list[0][0], point_list[0][1])
action_chains.w3c_actions.pointer_action.pointer_down()

依次移動經過各中間點

for point in point_list[1:]:
action_chains.w3c_actions.pointer_action.move_to_location(point[0], point[1])

移動到結束點並釋放

action_chains.w3c_actions.pointer_action.move_to_location(point_list[-1][0], point_list[-1][1])
action_chains.w3c_actions.pointer_action.pointer_up()

action_chains.w3c_actions.pointer_action._interval = 0

執行動作鏈

action_chains.perform()
time.sleep(2) # 等待幾秒,便於觀察曲線滑動效果

關閉 Appium 會話

driver.quit()

如上程式碼實現了曲線滑動但是動作過程太慢了,請問各位前輩們有什麼好的解決方法?