Appium + python - swipe滑屏操作实例
Posted teachertao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Appium + python - swipe滑屏操作实例相关的知识,希望对你有一定的参考价值。
方法一:
from appium import webdriver
from time import sleep
descred_caps =
"platformName":"android",
"platformVersion":"5.1.1",
"deviceName":"127.0.0.1:62001",
"appPackage":"com.baidu.yuedu",
"appActivity":"com.baidu.yuedu.splash.SplashActivity",
"noRset":"true",
"unicodeKeyboard":"true",
"resetKeyboard":"true"
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",descred_caps)
#获取屏幕size
size = driver.get_window_size()
print(size)
#屏幕的宽度 width
print(size["width"])
#屏幕的高度 height
print(size["height"])
def swipeUp(driver,t=500,n=1):
"""向上屏幕滑动"""
x1 = size["width"] * 0.5 # x坐标
y1 = size["height"] * 0.75 # 起点 y坐标
y2 = size["height"] * 0.25 # 终点 y 坐标
for i in range(n):
driver.swipe(x1,y1,x1,y2,t)
def swipeDown(driver,t=500,n=1):
"""向下屏幕滑动"""
x1 = size["width"] * 0.5 # x1 坐标
y1 = size["height"] * 0.25 # 起点y1坐标
y2 = size["height"] * 0.75 # 终点y2坐标
for i in range(n):
driver.swipe(x1,y1,x1,y2,t)
def swipeLeft(driver,t=500,n=1):
"""向左屏幕滑动"""
x1 = size["width"] * 0.75 # 起点x1坐标
y1 = size["height"] * 0.5 # y1 坐标
x2 = size["width"] * 0.25 # 终点x2坐标
for i in range(n):
driver.swipe(x1,y1,x2,y1,t)
def swipeRight(driver,t=500,n=1):
"""向右屏幕滑动"""
x1 = size["width"] * 0.25 #起点x1坐标
y1 = size["height"] * 0.5 # y1坐标
x2 = size["width"] * 0.75 #终点x2坐标
for i in range(n):
driver.swipe(x1,y1,x2,y1,t)
if __name__ == "__main__":
print(driver.get_window_size())
sleep(5)
swipeLeft(driver, n=2)
sleep(2)
swipeRight(driver, n=2)
driver.quit()
方法二:
#!usr/bin/env python
#!coding:utf-8
from appium import webdriver
import time as t
class Swipe(object):
def __init__(self,driver):
self.driver=driver
@property
def width(self):
return self.driver.get_window_size()[‘width‘]
@property
def height(self):
return self.driver.get_window_size()[‘height‘]
@property
def getResolution(self):
return str(self.width)+"*"+str(self.height)
@property
def set_Left_Right(self):
‘‘‘
:return: 实现从左到右滑动,滑动时X轴起点大于终点
‘‘‘
t.sleep(2)
self.driver.swipe(self.width*9/10,self.height/2,self.width/20,self.height/2,0)
@property
def set_Right_Left(self):
‘‘‘
:return:实现从右到左滑动,滑动时X轴起点小于终点
‘‘‘
t.sleep(2)
self.driver.swipe(self.width/10,self.height/2,self.width*9/10,self.height/2,0)
@property
def set_Up_Down(self):
‘‘‘
:return: 实现从上往下滑动,滑动时Y轴起点起点大于终点
‘‘‘
t.sleep(2)
self.driver.swipe(self.width/2,self.height*9/10,self.width/2,self.height/20,0)
@property
def set_Down_Up(self):
‘‘‘
:return: 实现从下往上滑动,滑动时Y轴起点小于终点
‘‘‘
t.sleep(2)
self.driver.swipe(self.width/2,self.height/20,self.width/2,self.height*9/10,0)
以上是关于Appium + python - swipe滑屏操作实例的主要内容,如果未能解决你的问题,请参考以下文章
appium+python自动化24-滑动方法封装(swipe)转载