selenium模拟H5触摸滑动之-TouchAction
最近做移动端H5页面的自动化测试时候,需要模拟一些上拉,下滑的操作,最初考虑使用使用selenium ActionChains来模拟操作,但是ActionChains 只是针对PC端程序鼠标模拟的一系列操作对H5页面操作时无效的,
比如:
# 将页面滚动条拖到底部,需要设置sleep(1)
sleep(1)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# driver.execute_script("window.scrollTo(0, 10000);")
# sleep(3)
后来阅读了下selenium的文档发现TouchAction可以对移动端页面自动化操作;
首先使用TouchAction的时候首先需要在头上引入该模块from selenium.webdriver.common.touch_actions import TouchActions
通过scroll_from_element、flick_element 方法来实现下拉操作
TouchAction提供的一些方法:
- double_tap(on_element) #双击
- flick_element(on_element, xoffset, yoffset, speed) #从元素开始以指定的速度移动
- long_press(on_element) #长按不释放
- move(xcoord, ycoord) #移动到指定的位置
- perform() #执行链中的所有动作
- release(xcoord, ycoord) #在某个位置松开操作
- scroll(xoffset, yoffset) #滚动到某个位置
- scroll_from_element(on_element, xoffset, yoffset) #从某元素开始滚动到某个位置
- tap(on_element) #单击
- tap_and_hold(xcoord, ycoord) #某点按住
因为我们模拟的是移动端的H5自动化测试,首先需要我们将浏览器设置成为手机浏览器(设置之后,模拟会更加真实)
1.以元素为起点向下滑动,实现下拉操作
scroll_from_element(on_element xoffset yoffset)
on_element:开始元素滚动。
xoffset:X偏移量。
yoffset:Y偏移量。
注意:向下滑动为负数,向上滑动为正数
import time
from selenium import webdriver
from selenium.webdriver.common.touch_actions import TouchActions
"""设置手机的大小"""
mobileEmulation = {'deviceName': 'Apple iPhone 5'}
options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', mobileEmulation)
driver = webdriver.Chrome(chrome_options=options)
driver.get('http://m.test.90dichan.com')
driver.maximize_window()
"""定位操作元素"""
button = driver.find_element_by_xpath('//*[@id="pullrefresh"]/div[2]/ul/li[2]/a/div[2]/span')
time.sleep(3)
Action = TouchActions(driver)
"""从button元素像下滑动200元素"""
Action.scroll_from_element(button, 0, -200).perform()
time.sleep(3)
driver.close()
2.以元素为起点用一定速度向下滑动,实现下拉操作
flick_element(on_element, xoffset, yoffset, speed);
on_element #操作元素定位
xoffset #x轴偏移量
yoffset #y轴偏移量
speed #速度
注意:向上滑动为负数,向下滑动为正数
import time
from selenium import webdriver
from selenium.webdriver.common.touch_actions import TouchActions
"""设置手机的大小"""
mobileEmulation = {'deviceName': 'Apple iPhone 5'}
options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', mobileEmulation)
driver = webdriver.Chrome(chrome_options=options)
driver.get('http://m.test.90dichan.com')
driver.maximize_window()
"""定位操作元素"""
button = driver.find_element_by_xpath('//*[@id="pullrefresh"]/div[2]/ul/li[2]/a/div[2]/span')
time.sleep(3)
Action = TouchActions(driver)
"""从button元素像下滑动200元素,以50的速度向下滑动"""
Action.flick_element(button, 0, 200, 50).perform()
time.sleep(3)
driver.close()
参考:
[python selenium TouchAction模拟移动端触摸操作]: https://www.cnblogs.com/mengyu/p/8136421.html
[ python+selenium滑动式验证码解决办法 ]: https://blog.csdn.net/zha6476003/article/details/79002430
[ python selenium 下拉列表定位 ]:https://blog.csdn.net/xm_csdn/article/details/53376839
selenium模拟H5触摸滑动之-TouchAction的更多相关文章
- selenium 模拟滑动解锁
来源:Selenium模拟JQuery滑动解锁 (selenium +Python ) 本文:selenium+Java package cn.gloryroad; import org.open ...
- python+selenium模拟京东登录后台
python+selenium模拟京东登录后台 import json from time import sleep from selenium import webdriver #from sele ...
- selenium如何处理H5视频
selenium处理H5视频主要使用的是javascript,javascript函数有内置的对象叫arguments,arguments包含了调用的参数组,[0]代表取第一个值. currentSr ...
- IOS的H5页面滑动不流畅的问题:
IOS的H5页面滑动不流畅的问题: -webkit-overflow-scrolling : touch; 需要滑动的是哪块区域,就在哪里加上这段代码就OK
- Swipe JS – 移动WEB页面内容触摸滑动类库
想必做移动前端的同学经常会接到这样子的一个需求,就是在移动设备页面上的banner图能够用手指触摸左右或上下的滑动切换,这在移动设备是个很常见的一个效果,其用户体验远甚于点击一个按钮区域,通过手指的触 ...
- 基于zepto的插件之移动端无缝向上滚动并上下触摸滑动
该插件乃本博客作者所写,目的在于提升作者的js能力,也给一些js菜鸟在使用插件时提供一些便利,老鸟就悠然地飞过吧. 公司的移动端项目是基于zepto的,有一个页面要求文字能够无缝地不停向上滚动,但查了 ...
- 移动端网站的内容触摸滑动-Swiper插件
手机平板等大多移动端站点都会有触摸滑动内容的功能,公司移动端站点(m.muzhiwan.com)的标题广告滑动以及轮播效果就是用的Swiper插件. Swiper就是常用于移动端网站的内容触摸滑动的一 ...
- android131 360 05 手势触摸滑动,sim卡,开机启动的广播,手机联系人,SharedPreferences,拦截短信
安卓手势触摸滑动: package com.itheima52.mobilesafe.activity; import android.app.Activity; import android.con ...
- 移动端触摸滑动插件Swiper
移动端触摸滑动插件Swiper 04/02/2015 一.了解Swiper 目前移动端项目一般都需要具有触屏焦点图的效果,如果你也需要实现这一功能的话,Swiper是一个不错的选择. 1.他不需要加载 ...
随机推荐
- plink修改正负链(--flip, change the positive and negative stand)
修改正负链用到的参数为--flip 假定trial.bim的内容如下: trial.bim 1 rs142578063 0 732746 G A 1 rs144022023 0 732801 G A ...
- KSQL Syntax Reference
KSQL Syntax Reference KSQL has similar semantics to SQL: Terminate KSQL statements with a semicolon ...
- [lodop]css样式after、before添加content内容之前和之后
css样式可以在内容之前和之后加内容.格式是:css类名:before{content:在之前加的内容}css类名:after{content:在之后加的内容}这种写法在LODOP里直接测试是不行的, ...
- C#给图片加水印,可设置透明度
C#给图片加水印,可设置透明度,设置水印的位置可以看一下上一篇哈 /// <summary> /// Creating a Watermarked Photograph with GDI+ ...
- 《ucore lab5》实验报告
资源 ucore在线实验指导书 我的ucore实验代码 练习1: 加载应用程序并执行(需要编码) 题目 do_execv函数调用load_icode(位于kern/process/proc.c中) 来 ...
- .net字符串内存的分配
几次面试中遇到都有类似的问题,就是 string str = "aa" + "bb" + "ccc";进行了几次内存分配? 1 class ...
- LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
- java通过jasper文件生成jpg图片
iReport资料紧俏,整理好就赶紧传上来: 工具类:JpgExport public class JpgExportUtil { public static String Export(Map< ...
- fpga基础
1.FPGA 的分类: 根据 FPGA 基本结构,可将其分为基于乘积项(Product-Term)技术的 FPGA 和基于查找表(Look-Up-Table)技术的 FPGA 两种. (1)基于乘积项 ...
- BJFU——205基于顺序存储结构的图书信息表的排序
#include<stdio.h> #include<stdlib.h> #define MAX 1000 typedef struct{ double no; char na ...