selenium屏幕操作事件TouchActions

说明:本篇博客基于selenium 4.1.0

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()

更多技术文章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值