selenium源码通读·11 |webdriver/common/touch_actions.py-TouchActions类分析

·11 |webdriver/common/touch_actions.py-TouchActions类分析

1 源码路径

selenium/webdriver/common/touch_actions.py

在这里插入图片描述

2 功能说明

class TouchActions(object):
    """
    Generate touch actions. Works like ActionChains; actions are stored in the
    TouchActions object and are fired with perform().
    """
  • 模拟移动端操作;
  • 类似ActionChains一样;
  • 动作存储在TouchActions对象中,并通过perform()触发使用。

3 __init__说明

    def __init__(self, driver):
        """
        Creates a new TouchActions object.

        :Args:
         - driver: The WebDriver instance which performs user actions.
           It should be with touchscreen enabled.
        """
        self._driver = driver
        self._actions = []
  • 创建新的TouchActions对象;
  • 执行用户操作的WebDriver实例,即传入driver。

4 perform说明

    def perform(self):
        """
        Performs all stored actions.
        """
        for action in self._actions:
            action()
  • 执行所有存储的操作。

5 所有API

API说明
tap(self, on_element)单击
double_tap(self, on_element)双击
tap_and_hold(self, xcoord, ycoord)在对应x,y坐标按住
move(self, xcoord, ycoord)移动到指定位置
release(self, xcoord, ycoord)在指定位置释放之前发出的tap_and_hold命令
scroll(self, xoffset, yoffset)滚动到某个位置
scroll_from_element(self, on_element, xoffset, yoffset)on_element元素开始,触摸并滚动到x,y偏移量
long_press(self, on_element)长按
flick(self, xspeed, yspeed)从屏幕任何地方开始,以x,y的速度(像素/秒)进行移动
flick_element(self, on_element, xoffset, yoffset, speed)从元素on_element开始,以x,y的速度(像素/秒)移动x,y偏移量

6 实例说明

# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/5/24
# 文件名称:selen_touch.py
# 作用:TouchActions类
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson


from time import sleep
from selenium import webdriver
from selenium.webdriver import TouchActions

"""
1、打开chrome,输入百度网址
2、搜索框输入“NoamaNelson”,点击搜索
3、上划页面到底部,点击“下一页”
"""
option = webdriver.ChromeOptions()
option.add_experimental_option('w3c', False)
driver = webdriver.Chrome(options=option)
driver.maximize_window()
driver.implicitly_wait(3)

driver.get("https://www.baidu.com")
el = driver.find_element_by_id("kw")
el_search = driver.find_element_by_id("su")
el.send_keys("NoamaNelson")
action = TouchActions(driver)
action.tap(el_search)
action.scroll_from_element(el, 0, 3000)
action.perform()
driver.find_element_by_css_selector('#page > div > a.n').click()
sleep(3)

『全栈测试技术,分享,共勉,共进,提升』


posted @   虫无涯  阅读(8)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示