Python Appium 滑动、点击等操作
Python Appium 滑动、点击等操作
1、手机滑动-swipe
# FileName : Tmall_App.py
# Author : Adil
# DateTime : 2018/3/25 17:22
# SoftWare : PyCharm from appium import webdriver caps = {} caps['platformName'] = 'Android'
caps['platformVersion'] = '6.0'
caps['deviceName'] = 'N79SIV5PVCSODAQC'
caps['appPackage'] = 'com.tmall.wireless'
caps['appActivity'] = 'com.tmall.wireless.splash.TMSplashActivity'
#隐藏键盘
caps['unicodeKeyboard'] = True
caps['resetKeyboard'] = True
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps) driver.swipe()
if __name__ == '__main__': pass
查看源码
Ctrl + 鼠标右键点击 driver.swipe()
# convenience method added to Appium (NOT Selenium 3)
def swipe(self, start_x, start_y, end_x, end_y, duration=None):
"""Swipe from one point to another point, for an optional duration. :Args:
- start_x - x-coordinate at which to start
- start_y - y-coordinate at which to start
- end_x - x-coordinate at which to stop
- end_y - y-coordinate at which to stop
- duration - (optional) time to take the swipe, in ms. :Usage:
driver.swipe(100, 100, 100, 400)
"""
# `swipe` is something like press-wait-move_to-release, which the server
# will translate into the correct action
action = TouchAction(self)
action \
.press(x=start_x, y=start_y) \
.wait(ms=duration) \
.move_to(x=end_x, y=end_y) \
.release()
action.perform()
return self
查看源码语法,起点和终点四个坐标参数。 手机屏幕从左上角开始为0,向右为x轴坐标,向下为y轴坐标。
duration是滑动屏幕持续的时间,时间越短速度越快。默认为None可不填,一般设置500-1000毫秒比较合适。
swipe(self, start_x, start_y, end_x, end_y, duration=None)
Swipe from one point to another point, for an optional duration.
从一个点滑动到另外一个点,duration是持续时间 :Args:
- start_x - 开始滑动的x坐标
- start_y - 开始滑动的y坐标
- end_x - 结束点x坐标
- end_y - 结束点y坐标
- duration - 持续时间,单位毫秒 :Usage:
driver.swipe(100, 100, 100, 400)
向下滑动实例
# FileName : Tmall_App.py
# Author : Adil
# DateTime : 2018/3/25 17:22
# SoftWare : PyCharm
import time
from appium import webdriver caps = {} caps['platformName'] = 'Android'
caps['platformVersion'] = '6.0'
caps['deviceName'] = 'N79SIV5PVCSODAQC'
caps['appPackage'] = 'com.tmall.wireless'
caps['appActivity'] = 'com.tmall.wireless.splash.TMSplashActivity'
#隐藏键盘
caps['unicodeKeyboard'] = True
caps['resetKeyboard'] = True
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps) # 获取屏幕的size
size = driver.get_window_size()
print(size)
# 获取屏幕宽度 width
width = size['width']
print(width)
# 获取屏幕高度 height
height = size['height']
print(height) # 执行滑屏操作,向下(下拉)滑动
x1 = width*0.5
y1 = height*0.25
y2 = height*0.9
time.sleep(3)
print("滑动前")
driver.swipe(x1,y1,x1,y2)
print("滑动后")
# 增加滑动次数,滑动效果不明显,增加滑动次数 for i in range(5):
print("第%d次滑屏"%i)
time.sleep(3)
driver.swipe(x1,y1,x1,y2)
time.sleep(3) driver.quit() if __name__ == '__main__': pass
封装滑动方法,代码如下:
# FileName : Tmall_App.py
# Author : Adil
# DateTime : 2018/3/25 17:22
# SoftWare : PyCharm
import time
from appium import webdriver caps = {} caps['platformName'] = 'Android'
caps['platformVersion'] = '6.0'
caps['deviceName'] = 'N79SIV5PVCSODAQC'
caps['appPackage'] = 'com.tmall.wireless'
caps['appActivity'] = 'com.tmall.wireless.splash.TMSplashActivity'
#隐藏键盘
caps['unicodeKeyboard'] = True
caps['resetKeyboard'] = True
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps) # 获取屏幕的size
size = driver.get_window_size()
print(size)
# 获取屏幕宽度 width
width = size['width']
print(width)
# 获取屏幕高度 height
height = size['height']
print(height) # 执行滑屏操作,向下(下拉)滑动
x1 = width*0.5
y1 = height*0.25
y2 = height*0.8
time.sleep(3)
print("滑动前")
driver.swipe(x1,y1,x1,y2)
print("滑动后")
# 增加滑动次数,滑动效果不明显,增加滑动次数 for i in range(5):
print("第%d次滑屏"%i)
time.sleep(3)
driver.swipe(x1,y1,x1,y2)
time.sleep(3) # 封装滑动方法 def swipeUp(driver,n = 5):
'''定义向上滑动方法'''
print("定义向上滑动方法")
x1 = width*0.5
y1 = height*0.9
y2 = height*0.25
time.sleep(3)
print("滑动前")
for i in range(n):
print("第%d次滑屏" % i)
time.sleep(3)
driver.swipe(x1, y1, x1, y2) def swipeDown(driver,n = 5):
'''定义向下滑动方法'''
print("定义向下滑动方法")
x1 = width*0.5
y1 = height*0.25
y2 = height*0.9
time.sleep(3)
print("滑动前")
for i in range(n):
print("第%d次滑屏" % i)
time.sleep(3)
driver.swipe(x1, y1, x1, y2) def swipeLeft(driver,n = 5):
'''定义向左滑动方法'''
print("定义向左滑动方法")
x1 = width*0.8
x2 = width*0.2
y1 = height*0.5 time.sleep(3)
print("滑动前")
for i in range(n):
print("第%d次滑屏" % i)
time.sleep(3)
driver.swipe(x1, y1, x2, y1) def swipeRight(driver,n = 5):
'''定义向右滑动方法'''
print("定义向右滑动方法")
x1 = width*0.2
x2 = width*0.8
y1 = height*0.5 time.sleep(3)
print("滑动前")
for i in range(n):
print("第%d次滑屏" % i)
time.sleep(3)
driver.swipe(x1, y1, x2, y1) if __name__ == '__main__': swipeUp(driver)
swipeDown(driver)
swipeLeft(driver)
swipeRight(driver) driver.quit()
2、点击手机屏幕坐标-tap
使用场景:有时候定位元素的时候,你使出了十八班武艺还是定位不到,怎么办呢?(面试经常会问)
那就拿出绝招:点元素所在位置的坐标
import time
from appium import webdriver caps = {} caps['platformName'] = 'Android'
caps['platformVersion'] = '6.0'
caps['deviceName'] = 'N79SIV5PVCSODAQC'
caps['appPackage'] = 'com.tmall.wireless'
caps['appActivity'] = 'com.tmall.wireless.splash.TMSplashActivity'
#隐藏键盘
caps['unicodeKeyboard'] = True
caps['resetKeyboard'] = True
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps) driver.tap()
查看源码
Ctrl + 鼠标右键点击 driver.tap()
# convenience method added to Appium (NOT Selenium 3)
def tap(self, positions, duration=None):
"""Taps on an particular place with up to five fingers, holding for a
certain time :Args:
- positions - an array of tuples representing the x/y coordinates of
the fingers to tap. Length can be up to five.
- duration - (optional) length of time to tap, in ms :Usage:
driver.tap([(100, 20), (100, 60), (100, 100)], 500)
"""
if len(positions) == 1:
action = TouchAction(self)
x = positions[0][0]
y = positions[0][1]
if duration:
action.long_press(x=x, y=y, duration=duration).release()
else:
action.tap(x=x, y=y)
action.perform()
else:
ma = MultiAction(self)
for position in positions:
x = position[0]
y = position[1]
action = TouchAction(self)
if duration:
action.long_press(x=x, y=y, duration=duration).release()
else:
action.press(x=x, y=y).release()
ma.add(action) ma.perform()
return self
tap是模拟手指点击,一般页面上元素
的语法有两个参数,第一个是positions,是list类型最多五个点,duration是持续时间,单位毫秒
tap(self, positions, duration=None):
Taps on an particular place with up to five fingers, holding for a certain time
模拟手指点击(最多五个手指),可设置按住时间长度(毫秒)
:Args:
- positions - list类型,里面对象是元组,最多五个。如:[(100, 20), (100, 60)]
- duration - 持续时间,单位毫秒,如:500
:Usage:
driver.tap([(100, 20), (100, 60), (100, 100)], 500)
实际应用:坐标定位
如图:查看元素坐标,可以看到右侧bonds属性

代码实例如下:
# FileName : Tamll_App_TapDemo.py
# Author : Adil
# DateTime : 2018/3/26 17:44
# SoftWare : PyCharm import time
from appium import webdriver caps = {} caps['platformName'] = 'Android'
caps['platformVersion'] = '6.0'
caps['deviceName'] = 'N79SIV5PVCSODAQC'
caps['appPackage'] = 'com.tmall.wireless'
caps['appActivity'] = 'com.tmall.wireless.splash.TMSplashActivity'
#隐藏键盘
caps['unicodeKeyboard'] = True
caps['resetKeyboard'] = True
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps) # 操作元素坐标点击
# 天猫-天猫超市 坐标
def tapHit(driver):
time.sleep(3)
driver.tap([(234,324),(438,561)],500)
time.sleep(2) if __name__ == '__main__':
tapHit(driver) time.sleep(15)
driver.quit()
操作效果如下:

说明:
通过坐标定位是元素定位的下下下策,实在没办法才用这个,另外如果换了手机分辨率,这个坐标就不能写死了,得算出所在屏幕的比例。
Python Appium 滑动、点击等操作的更多相关文章
- python+appium模拟手机物理按键操作
一句代码:driver.keyevent() 括号里填入的是手机物理按键的数字代号 driver.press_keycode() 括号里填入的是键盘按键的数字代号 手机物理 ...
- Python+Appium自动化测试(10)-TouchAction类与MultiAction类(控件元素的滑动、拖动,九宫格解锁,手势操作等)
滑动屏幕方法swipe一般用于对页面进行上下左右滑动操作,但自动化过程中还会遇到其他情况,如对控件元素进行滑动.拖拽操作,九宫格解锁,手势操作,地图的放大与缩小等.这些需要针对控件元素的滑动操作,或者 ...
- Python + Appium 自动化操作微信入门看这一篇就够了
简介 Appium 是一个开源的自动化测试工具,支持 Android.iOS 平台上的原生应用,支持 Java.Python.PHP 等多种语言. Appium 封装了 Selenium,能够为用户提 ...
- appium滑动操作(向上、向下、向左、向右)
appium滑动操作(向上滑动.向下滑动.向左滑动.向右滑动) 测试app:今日头条apk 测试设备:夜游神模拟器 代码如下: 先用x.y获取当前的width和height def getSize() ...
- Python + Appium 环境搭建
---恢复内容开始--- Appium自动化公司内部测试培训1-环境搭建 课程目的 一.Python + Appium 环境搭建 课程内容 1 安装前准备工作 搭建环境所需要的安装文件已经下载好 ...
- Python +appium baseview
封装python+appium 的baseview模块 from selenium.webdriver.support.ui import WebDriverWait from time import ...
- Python+Appium自动化测试(15)-使用Android模拟器(详细)
做APP的UI自动化测试时,我们往往会使用真机跑自动化测试脚本,因为这样才是最真实的使用场景.但前期调试脚本的话,可以先使用模拟器,这样相对更加方便. 不推荐使用Android SDK里自带模拟器,太 ...
- python+appium运行提示找不到adb.exe “An unknown server-side error occurred while processing the command. Original error: Could not find 'adb.exe' in ["D:\\adt\\sdk;\\platform-tools\\adb.exe"”
自己踩过的坑,不记下来就会忘掉,忘了就会不断的重复踩坑!! 重来在一台电脑上搭建了python的环境,在运行的时候,提示找不到adb.exe,看到这个问题我在想是不是我的环境变量配置有问题,我就去改了 ...
- Python appium搭建app自动化测试环境
appium做app自动化测试,环境搭建是比较麻烦的. 也是很多初学者在学习app自动化之时,花很多时间都难跨越的坎. 但没有成功的环境,就没有办法继续后续的使用. 在app自动化测试当中,我们主要是 ...
随机推荐
- Codeforces 995F Cowmpany Cowmpensation - 组合数学
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个棵$n$个点的有根树和整数$D$,给这$n$个点标号,要求每个节点的标号是正整数,且不超过父节点的标号,根节点的标号不得超过D. 很容 ...
- topcoder srm 445 div1
problem1 link 这个的结论是只需要考虑坐标是整数或者是整数.5,比如(2.5,3),(4,3.5),(1.5,4.5)这样的时候.这个详细证明起来应该挺麻烦的.这里有一些讨论. probl ...
- Python3 tkinter基础 LabelFrame StringVar 单击按钮,Label中显示的文字更换
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Git rebase的使用
rebase 在 Git 中整合来自不同分支的修改主要有两种方法:merge 以及 rebase. 在本节中我们将学习什么是“rebase”,怎样使用“rebase”,并将展示该操作的惊艳之处,以及指 ...
- PyCharm史上最强攻略
Ctrl + D 复制选定的区域或行 Ctrl + Y 删除选定的行 Ctrl + Alt + L 代码格式化 Ctrl + Alt + O 优化导入(去掉用不到的包导入) Ctrl + 鼠标 简介/ ...
- Django框架(六) Django之模板继承
模版导入和继承 模版导入 一个页面只能继承一个模板,如何解决了?如何使用多个模板,或者引入其他页面 <% include "a.html" %> 可以引用多次 模板,i ...
- Android灯光系统--通知灯深入分析【转】
本文转自:https://www.cnblogs.com/lkq1220/p/6406261.html Android灯光系统--通知灯深入分析 通知的类别 声音 振动 闪灯 APP如何发出通知灯请求 ...
- luoguP4072 [SDOI2016]征途
[SDOI2016]征途 大体 大概就是推推公式,发现很傻逼的\(n^3\)DP get60 进一步我们发现状态不能入手,考虑优化转移 套个斜率优化板子 每一层转移来一次斜率优化 思路 先便便式子 \ ...
- Kubernetes(k8s)入门、单机版安装、kuberctl指令、k8s服务实例
1.切换root .关闭centos自带的防火墙 # systemctl disable firewalld # systemctl stop firewalld .安装etcd和kubernetes ...
- Newcoder 华华给月月出题(线筛)题解
题目描述: 华华刚刚帮月月完成了作业.为了展示自己的学习水平之高超,华华还给月月出了一道类似的题: Ans=⊕Ni=1(iNmod(109+7))Ans=⊕i=1N(iNmod(109+7)) ⊕⊕符 ...