selenium Webdriver自动化测试之手势操作TouchAction 详解
2023-02-18 16:37:25 时间
TouchAction,类似于ActionChains,ActionChains只是针对PC端程序鼠标模拟的一系列操作,对H5页面操作是无效的。TouchAction可以对H5页面操作,通过TouchAction可以实现点击、滑动、拖拽、多点触控,以及模拟手势等各种操作。
关于 ActionChains的介绍可移步: https://www.cnblogs.com/feng0815/p/8344120.html
手势控制
1、按压控件
方法:
- press()
开始按压一个元素或坐标点(x,y)。通过手指按压手机屏幕的某个位置。
press(WebElement el, int x, int y)
press也可以接收屏幕的坐标(x,y)。
例:TouchAction(driver).press(x=0,y=308).release().perform()
除了press()方法之外,本例中还用到了别外两个新方法。
-
release() 结束的行动取消屏幕上的指针。
-
Perform() 执行的操作发送到服务器的命令操作。
2、长按控件
方法:
- longPress()
开始按压一个元素或坐标点(x,y)。 相比press()方法,longPress()多了一个入参,既然长按,得有按的时间吧。duration以毫秒为单位。1000表示按一秒钟。其用法与press()方法相同。
longPress(WebElement el, int x, int y, Duration duration)
例:
-
TouchAction action = new TouchAction(driver);
-
action.longPress(names.get(200),1000).perform().release();
-
action.longPress(200 ,200,1000).perform().release();
3、点击控件
方法:
- tap()
对一个元素或控件执行点击操作。用法参考press()。
tap(WebElement el, int x, int y)
例:
-
TouchAction action = new TouchAction(driver);
-
action.tap(names.get(200)).perform().release();
-
action.tap(200 ,200).perform().release();
4、移动
方法:
- moveTo()
将指针(光标)从过去指向指定的元素或点。
movTo(WebElement el, int x, int y)
其用法参考press()方法。
例:
-
TouchAction action = new TouchAction(driver);
-
action.moveTo(names.get(200)).perform().release();
-
action.moveTo(200 ,200).perform().release();
5、暂停
方法:
- wait()
暂停脚本的执行,单位为毫秒。
action.wait(1000);
#!/usr/bin/python # -*- coding: UTF-8 -*- """ @author:chenshifeng @file:test_TouchAction.py @time:2020/10/17 """ import time from selenium import webdriver from selenium.webdriver import TouchActions class TestTouchAction(): def setup_method(self): option = webdriver.ChromeOptions() option.add_experimental_option('w3c', False) self.driver = webdriver.Chrome(options=option) self.driver.maximize_window() self.driver.implicitly_wait(5) def teardown_method(self): self.driver.quit() def test_touchaction_scrollbottom(self): self.driver.get("https://www.baidu.com/") el = self.driver.find_element_by_id('kw') el_search = self.driver.find_element_by_id('su') el.send_keys('selenium测试') action = TouchActions(self.driver) action.tap(el_search) # 点击 action.perform() action.scroll_from_element(el, 0, 10000).perform() time.sleep(3)
end
相关文章
- [PHP] php作为websocket的客户端实时读取推送日志文件
- [MySQL] in 子查询出现DEPENDENT SUBQUERY问题
- [MySQL] 导入数据时防止出现乱码
- [MySQL] group by 聚合函数的原理和聚合限制原因SELECT list is not in GROUP BY clause and contains nonaggregated column
- [PHP] websocket协议的生成
- [MySQL]mysql的ANY_VALUE()函数 解决 ONLY_FULL_GROUP_BY 模式
- [PHP] 框架中.env文件的加载过程
- [PHP] PHP7已经删除了preg_replace的e修饰符
- [日常]解决Connection to `ssl://pecl.php.net:443' failed
- [日常]k8s的前世今生
- [日常]windows下kill进程工具taskkill
- [CSS] 纯CSS的前端图标icon库并且修改大小和颜色
- [PHP] php中的索引数组和数组顺序问题
- [PHP] RBAC权限与审批流的简单数据库构想
- [日常] 修改编辑word中的页眉页脚
- [PHP] socket客户端时的超时问题
- [nginx]配置nginx支持websocket解决返回400错误问题
- [nginx]配置nginx支持websocket解决返回400错误问题
- [PHP] php5.3之前-php5.3-php7垃圾回收机制的进化
- [PHP] include语句的注意事项