zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

2023-03-07 09:06:00 时间

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)