python selenium TouchAction模拟移动端触摸操作(十八)
最近做移动端H5页面的自动化测试时候,需要模拟一些上拉,下滑的操作,最初考虑使用使用selenium ActionChains来模拟操作,但是ActionChains 只是针对PC端程序鼠标模拟的一系列操作对H5页面操作时无效的,后来阅读了下selenium的文档发现TouchAction可以对移动端页面自动化操作;
首先使用TouchAction的时候首先需要在头上引入该模块
from selenium.webdriver.common.touch_actions import TouchActions
通过scroll_from_element、flick_element 方法来实现下拉操作
因为我们模拟的是移动端的H5自动化测试,首先需要我们将浏览器设置成为手机浏览器;
1.以元素为起点以一定速度向下滑动,实现下拉操作
- flick_element(on_element, xoffset, yoffset, speed);
- on_element #操作元素定位
- xoffset #x轴偏移量
- yoffset #y轴偏移量
- speed #速度
注意:
向上滑动为负数,向下滑动为正数
# -*- coding: utf-8 -*-
# @Time : 2017/12/28 10:26
# @Author : Hunk
# @File : ex86.py.py
# @Software: PyCharm
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()
2.以元素为起点向下滑动,实现下拉操作
- scroll_from_element(on_element xoffset yoffset)
- on_element:开始元素滚动。
- xoffset:X偏移量。
- yoffset:Y偏移量。
注意:
向下滑动为负数,向上滑动为正数
# -*- coding: utf-8 -*-
# @Time : 2017/12/28 10:26
# @Author : Hunk
# @File : ex86.py.py
# @Software: PyCharm
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()
下面我们看下下拉的效果

下面看一下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) #某点按住
其实上述的部分方法与ActionChains 中的鼠标操作很相似,由于与鼠标操作的书写方法一致的,所以在这里不详细介绍,具体书写方法大家可以参考下【python selenium-webdriver 元素操作之鼠标操作(四)】内容,下面我们主要来介绍下移动的操作。
python selenium TouchAction模拟移动端触摸操作(十八)的更多相关文章
- Python+Selenium自动化-模拟键盘操作
Python+Selenium自动化-模拟键盘操作 0.导入键盘类Keys() selenium中的Keys()类提供了大部分的键盘操作方法:通过send_keys()方法来模拟键盘上的按键. # ...
- Python+Selenium自动化 模拟鼠标操作
Python+Selenium自动化 模拟鼠标操作 在webdriver中,鼠标的一些操作如:双击.右击.悬停.拖动等都被封装在ActionChains类中,我们只用在需要使用的时候,导入这个类就 ...
- Python + Selenium + AutoIt 模拟键盘实现另存为、上传、下载操作详解
前言 在web页面中,可以使用selenium的定位方式来识别元素,从而来实现页面中的自动化,但对于页面中弹出的文件选择框,selenium就实现不了了,所以就需引用AutoIt工具来实现. Auto ...
- Python+Selenium笔记(九):操作警告和弹出框
#之前发的 driver.switch_to_alert() 这句虽然可以运行通过,但是会弹出警告信息(这种写法3.x不建议使用) 改成 driver.switch_to.alert就不会了. (一 ...
- 【Selenium02篇】python+selenium实现Web自动化:鼠标操作和键盘操作!
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第二篇博 ...
- Python+selenium 模拟wap端页面操作
from selenium.webdriver.chrome.options import OptionsmobileEmulation = {'deviceName': 'iPhone X'}opt ...
- 使用python selenium webdriver模拟浏览器
selenium是进行web自动化测试的一个工具,支持C,C++,Python,Java等语言,他能够实现模拟手工操作浏览器,进行自动化,通过webdriver驱动浏览器操作,我使用的是chrome浏 ...
- python+selenium一:对浏览器的操作
# 1.打开Firefox浏览器from selenium import webdriverdriver = webdriver.Firefox()driver.get("https://w ...
- python selenium Chrome模拟手机浏览器
在做移动端页面测试时可以利用Chrome mobile emulation 辅助完成页面的适配问题,但是目前手机市场上的型号居多我们也没有办法通过人工的模式一一的去适配,所以这里考虑到通过自动化的模式 ...
随机推荐
- 快速比较 Kafka 与 Message Queue 的区别
https://hackernoon.com/a-super-quick-comparison-between-kafka-and-message-queues-e69742d855a8 A supe ...
- 自动化运维之Saltstack
第三十八课 自动化运维之Saltstack 目录 一.自动化运维介绍 二. saltstack安装 三. 启动saltstack服务 四. saltstack配置认证 五. saltstack远程执行 ...
- ftp远端上的文件下载
用linux下的wge下t载比在window下一个个点方便多了,命令如下: wget ftp:// 110.110.110/\*.mseed --ftp-user=username --ftp-pas ...
- browser_action' is only allowed for extensions but this is a legacy packaged app
manifest.json中不可包含调试代码: "app": { "launch": { "local_path": ...
- 微信小程序swiper 前后边距的使用
小程序中有一个组件swiper 就是滑块视图容器 其中提供了两个属性 previous-margin:前边距,可用于露出前一项的一小部分 next-margin:后边距,可用于露出后一项的 ...
- 解决使用mybatis分页插件PageHelper的一个报错问题
在项目中使用PageHelper进行分页,启动运行时报错,报错如下: 27-Aug-2017 09:58:48.017 INFO [localhost-startStop-1] org.apache. ...
- 二叉树:B+tree等
二叉树:(树是一种可以递归定义数据结构) 度:节点的个数 深度:层数(即从根点到叶子节点的层数) 满二叉树:指底层叶子节点左右均存在的二叉树. 完全二叉树:指底层叶子节点的右侧均存在的二叉树. 一般二 ...
- 关于实时监听input的值得变化的问题
onchange 关于input的onchange事件 其实是有出发条件的 并非实时监听的 1.鼠标点击事件 或者键盘事件(tab和wins键都可以触发 enter在ie9时不触发,火狐和ch ...
- Angular4.0中常用指令
Angular 4.0 1. 指令:*ngFor 循环带索引的两种方法: <tr *ngFor="let item of listWorkingDetails index as i&q ...
- Delphi编程之爬取贴吧多页帖子图片
接着上一篇文章说一下我们如何爬取多页帖子的图片. 第一步,还是随便找个贴吧,这次我们就找图片相对较少的射雕英雄传吧. 把页面拉到最底下,在页码区域单击右键,选择检查. 在这里我们看到了页码的代码区域, ...