selenium源码通读·11| 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 a...
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)
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)