zl程序教程

您现在的位置是:首页 >  Java

当前栏目

软件测试|selenium屏幕操作事件TouchActions

2023-02-18 16:46:25 时间

<font size='3'>说明:本篇博客基于selenium 4.1.0</font>

TouchActions说明

ActionChains都是selenium的一个模块,提供模拟屏幕操作的功能

TouchActions使用说明

使用方法与ActionChains一致:

# 步骤1:实例化一个TouchActions动作容器
actions = TouchActions(driver)

# 步骤2:往动作容器中依次添加动作
actions.tap(ele_click)                              # 调用的动作都会添加到动作容器中
actions.double_tap(ele_drag).scroll(0, 1000)        # 链式添加动作。每个动作返回值为容器对象,因此支持链式连续添加

# 步骤3:执行动作
actions.perform()

TouchActions

import time
from selenium import webdriver
from selenium.webdriver import TouchActions
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.implicitly_wait(5)
driver.get('https://www.baidu.com/')

actions = TouchActions(driver)
ele_input = driver.find_element(By.ID, "kw")
location = ele_input.location
'''
ActionChains格式,使用方法与ActionChains一致:
1. 实例化一个TouchActions动作容器
actions = TouchActions(driver)

2. 往动作容器添加动作
2.1 直接添加
actions.tap(ele_click)                              # 调用的动作都会添加到动作容器中
actions.double_tap(ele_drag).scroll(0, 1000)        # 链式添加动作。每个动作返回值为容器对象,因此支持链式连续添加

3. 执行动作
actions.perform()
'''

# 鼠标点击
actions.tap(ele_input)                          # 单击元素
actions.double_tap(ele_input)                   # 双击元素
actions.long_press(ele_input)                   # 长按元素


# 鼠标滚动
x, y = 30, 50
actions.scroll(x, y)                            # 滚动,向下向右(x, y)距离
actions.scroll_from_element(ele_input, x, y)    # 滚动,以元素为起点向下向右(x, y)距离

# 鼠标移动
x, y = 0, 1000
actions.move(x, y)                              # 鼠标移动到坐标(x,y)

# 释放鼠标
actions.release(x, y)                           # 鼠标移动坐标(x,y),并释放鼠标

# 滑动
xspeed, yspeed = 30, 50
actions.flick(xspeed, yspeed)                               # 滑动,向下向右以(xspeed, yspeed)速度滑动

xoffset, yoffset, speed = 30, 50, 20
actions.flick_element(ele_input, xoffset, yoffset, speed)   # 在元素处滑动,向下向右以speed的速度滑动(xoffset, yoffset)距离

# 拖动
x, y = 30, 50
actions.tap_and_hold(x, y)                      # 点住,在坐标(x, y)处


# 容器相关
actions.perform()                               # 按顺序开始执行动作


time.sleep(5)
driver.quit()