swipe
Appium使用滑动操作用到了swipe方法,定义如下:
swipe(self, start_x, start_y, end_x, end_y, duration=None)
从一个点滑动到另外一个点
start_x 是开始滑动的x坐标, start_y 是开始滑动的y坐标
end_x 是结束点x坐标,end_y是结束点y坐标
duration是持续时间,单位毫秒,可以不填,一般设置为500-1000之间吧
#水平向右滑动
比如driver.swipe(100,500,600,500,500)
获取屏幕大小
由于每个手机终端的分辨率都不一样,所以再调用的时候可以先获取到手机的屏幕尺寸
1 from appium import webdriver 2 desired_caps = { 3 "platformName": "Android", 4 "platformVersion": "5.1", 5 "deviceName": "U4KF9HSK99999999", 6 "appPackage": "com.taobao.taobao", 7 "appActivity": "com.taobao.tao.welcome.Welcome", 8 "unicodeKeyboard":True, 9 "resetKeyboard":True, 10 "noReset": True 11 } 12 driver = webdriver.Remote("http://localhost:4723/wd/hub",desired_caps) 13 14 # 获取屏幕的size 15 size = driver.get_window_size() 16 print(size)
输出结果:
{'height': 1920, 'width': 1080}
封装一下代码:
# -*- coding: utf-8 -*-# from appium import webdriver import time desired_caps = { "platformName": "Android", "platformVersion": "5.1", "deviceName": "U4KF9HSK99999999", "appPackage": "com.taobao.taobao", "appActivity": "com.taobao.tao.welcome.Welcome", "unicodeKeyboard":True, "resetKeyboard":True, "noReset": True } driver = webdriver.Remote("http://localhost:4723/wd/hub",desired_caps) def get_size():
#获取窗口尺寸 size=driver.get_window_size() x=size['width'] y=size['height'] return x,y def swipe_up(): #向上滑动 size = get_size() x1=int(size[0]*0.5) y1=int(size[1]*0.9) y2=int(size[1]*0.1) driver.swipe(x1,y1,x1,y2,500) def swipe_down(): #向下滑动 size = get_size() x1=int(size[0]*0.5) y1=int(size[1]*0.1) y2=int(size[1]*0.9) driver.swipe(x1,y1,x1,y2,500) def swipe_left(): #向左滑动 size = get_size() x1=int(size[0]*0.9) x2=int(size[0]*0.1) y1=int(size[1]*0.5) driver.swipe(x1,y1,x2,y1,500) def swipe_right(): #向右滑动 size = get_size() x1=int(size[0]*0.1) x2=int(size[0]*0.9) y1=int(size[1]*0.5) driver.swipe(x1,y1,x2,y1,500) if __name__ == "__main__": print(get_size()) for i in range(2): swipe_up() time.sleep(2) swipe_down() time.sleep(2) swipe_left() time.sleep(2) swipe_right() time.sleep(2)