click(on_element=None) ——單擊鼠標左鍵 click_and_hold(on_element=None) ——點擊鼠標左鍵,不鬆開 context_click(on_element=None) ——點擊鼠標右鍵 double_click(on_element=None) ——雙擊鼠標左鍵 drag_and_drop(source, target) ——拖拽到某個元素而後鬆開 drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某個座標而後鬆開 key_down(value, element=None) ——按下某個鍵盤上的鍵 key_up(value, element=None) ——鬆開某個鍵 move_by_offset(xoffset, yoffset) ——鼠標從當前位置移動到某個座標 move_to_element(to_element) ——鼠標移動到某個元素 move_to_element_with_offset(to_element, xoffset, yoffset) ——移動到距某個元素(左上角座標)多少距離的位置 perform() ——執行鏈中的全部動做 release(on_element=None) ——在某個元素位置鬆開鼠標左鍵 send_keys(*keys_to_send) ——發送某個鍵到當前焦點的元素 send_keys_to_element(element, *keys_to_send) ——發送某個鍵到指定元素
兩種實現方式python
#前面無聊的代碼我就略咯 #方式一: actions=ActionChains(driver) #拿到動做鏈對象 actions.drag_and_drop(sourse,target).perform() #其中sourse爲起始元素對象,target爲結束位置元素對象 #通常用於把一個圖片從一個地方拖到另外個元素地方 #方式二: actions=ActionChains(driver) #拿到動做鏈對象 actions.click_and_hold(移動的對象).perform() actions.move_by_offset(x,y).perform() #x爲移動水平距離,y爲移動垂直距離,正負表明左右或者上下 actions.release().perform() #鬆開,這一步別忘了
給個段代碼哈app
def get_stacks(distance): #distance爲滑動的總長度 distance += 20 ''' 勻加速\減速運行 v = v0 + a * t 位移: s = v * t + 0.5 * a * (t**2) ''' # 初速度 v0 = 0 # 加減速度列表 a_list = [20,40, 5] # 時間 t = 0.1 # 初始位置 s = 0 # 向前滑動軌跡 forward_stacks = [] mid = distance * 3 / 5 while s < distance: if s < mid: a = a_list[random.randint(0, 1)] else: a = -a_list[2] v = v0 stack = v * t + 0.5 * a * (t ** 2) # 每次拿到的位移 stack = round(stack) s += stack v0 = v + a * t forward_stacks.append(stack) back_stacks = [-1, -1, -2, -3, -2, -3, -2, -2, -3, -1] return {'forward_stacks': forward_stacks, 'back_stacks': back_stacks} stacks = get_stacks(60) print(stacks) forward_stacks = stacks['forward_stacks'] back_stacks = stacks['back_stacks'] slider_button = driver.find_element_by_class_name('滑動東西的類名') ActionChains(driver).click_and_hold(slider_button).perform() for forward_stack in forward_stacks: ActionChains(driver).move_by_offset(xoffset=forward_stack, yoffset=0).perform() for back_stack in back_stacks: ActionChains(driver).move_by_offset(xoffset=back_stack, yoffset=0).perform() time.sleep(0.3) ActionChains(driver).move_by_offset(xoffset=5, yoffset=0).perform() time.sleep(0.2) ActionChains(driver).move_by_offset(xoffset=-5, yoffset=0).perform() time.sleep(0.1) ActionChains(driver).move_by_offset(xoffset=1, yoffset=0).perform() time.sleep(0.1) ActionChains(driver).move_by_offset(xoffset=-1, yoffset=0).perform() ActionChains(driver).release().perform()