0


Selenium中ActionChains讲解

简介:

ActionChains是模拟鼠标的一些操作

web自动化中的常用操作:

driver.click():元素点击

driver.send_keys():输入

driver.swipe(start_x, start_y, end_x, end_y):根据坐标滑动(其实swipe的源码就是用的ActionChains)

而在APP自动化中,往往可能会有连续的操作,这时就需要用到ActionChains

Python中引入库方法:

#引入方式一
from selenium.webdriver.common.action_chains import ActionChains

#引入方式二
from selenium.webdriver import ActionChains

ActionChains方法列表

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() ——执行链中的所有动作(每次要执行ActionChains时都要加perform才会执行)

release(on_element=None) ——在某个元素位置松开鼠标左键

send_keys(*keys_to_send) ——发送某个键到当前焦点的元素

send_keys_to_element(*element, keys_to_send) ——发送某个键到指定元素

swipe的源码解析

def swipe(self: T, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0) -> T:
     # 创建手指touch(ActionChains默认是模拟鼠标操作mouse)  
     touch_input = PointerInput(interaction.POINTER_TOUCH, "touch")
     
     # 实例化ActionChains
     actions = ActionChains(self)
     
     # 将mouse换成touch
     actions.w3c_actions = ActionBuilder(self, mouse=touch_input)
     # 移动到某坐标点
     actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
     # 按下
     actions.w3c_actions.pointer_action.pointer_down()
     # 滑动时间
     if duration > 0:
         actions.w3c_actions = ActionBuilder(self, mouse=touch_input, duration=duration)
     # 移动到某坐标点
     actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)
     # 抬起
     actions.w3c_actions.pointer_action.release()
     # 执行
     actions.perform()
     return self

其实我们可以在此基础上进行添加修改,在抬起前面加多个移动就可以实现连续多点滑动的操作

def swipe(self, x0、y0、x1、y1、x2、y2)

     # 创建手指touch(ActionChains默认是模拟鼠标操作mouse)  
     touch_input = PointerInput(interaction.POINTER_TOUCH, "touch")
     
     # 实例化ActionChains
     actions = ActionChains(self)
     
     # 将mouse换成touch
     actions.w3c_actions = ActionBuilder(self, mouse=touch_input)
     # 移动到某坐标点
     actions.w3c_actions.pointer_action.move_to_location(x0, y0)
     # 按下
     actions.w3c_actions.pointer_action.pointer_down()
     
     # 移动到某坐标点
     actions.w3c_actions.pointer_action.move_to_location(x1, y1)
     actions.w3c_actions.pointer_action.move_to_location(x2, y2)
     # 抬起
     actions.w3c_actions.pointer_action.release()
     # 执行
     actions.perform()
     
     传几个点就只能移动几次。

当然也可以把 "抬起" 与前面的"移动"代码拆分开进行单独的封装,代码如下:

    def new_touch(self, x, y):     # 按下第一个点
        touch_input = PointerInput(interaction.POINTER_TOUCH, "touch")

        actions = ActionChains(self.driver)
        actions.w3c_actions = ActionBuilder(self, mouse=touch_input)
        actions.w3c_actions.pointer_action.move_to_location(x, y)
        actions.w3c_actions.pointer_action.pointer_down()
        

    def swipe_touch(self, x, y):   # 移动到某点,可以多次执行即可完成多点滑动

        actions.w3c_actions.pointer_action.pause(0)  #
        actions.w3c_actions.pointer_action.move_to_location(x, y)

    def perform_touch(self):     # 抬起并执行
        
        actions.w3c_actions.pointer_action.release()  # 抬起
        actions.perform()  # 执行动作
        

还有一种是不用w3c标准的,直接用ActionChains中的方法

ac = ActionChains(Android)
    ac.move_to_element(el1)
    ac.click_and_hold()
    ac.move_to_element(el2)
    ac.move_to_element(el3)
    ac.move_to_element(el4)
    ac.move_to_element(el5)
    ac.move_to_element(el6)
    ac.move_to_element(el7)
    ac.move_to_element(el8)
    ac.move_to_element(el9)
    ac.release()
    ac.perform()

本文转载自: https://blog.csdn.net/m0_70349389/article/details/128482661
版权归原作者 搬砖小张! 所有, 如有侵权,请联系我们删除。

“Selenium中ActionChains讲解”的评论:

还没有评论