Appium学习笔记||8、滑动页面

1、方法

  Appium的swipe方法:swipe(self, start_x, start_y, end_x, end_y, duration=xxx)android

 

2、能够先获取元素坐标,而后根据上述方法滑动

  获取元素坐标方法,能够去appium或者uiautomator中,经过鼠标查看x,y的坐标值。web

  

 

from appium import webdriver
import time

desired_caps = {}
desired_caps['platformName'] = 'Android'#运行平台
desired_caps['platformVersion'] = '8.1.0'#安卓版本
desired_caps['deviceName'] = 'test'#设备名称,不重要
desired_caps['appPackage'] = 'com.tencent.qqlive'#Package名字
desired_caps['appActivity'] = '.ona.activity.WelcomeActivity'#appActivity名字
desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True
desired_caps['noReset'] = True
desired_caps['newCommandTimeout'] = 6000

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
driver.implicitly_wait(10)

time.sleep(5)

for i in range(5):
driver.swipe(200, 500, 900, 500, 1000)#纵坐标不动,只有横坐标变化,这里是从左向右滑
time.sleep(1)

 

 3、可是因为设备不一样,坐标也不一样。

  为了不这个问题,最好能够先获取当前页面的尺寸,而后再根据当前屏幕的尺寸进行定位。app

 1 from appium import webdriver
 2 import time
 3 
 4 desired_caps = {}
 5 desired_caps['platformName'] = 'Android'#运行平台
 6 desired_caps['platformVersion'] = '8.1.0'#安卓版本
 7 desired_caps['deviceName'] = 'test'#设备名称,不重要
 8 desired_caps['appPackage'] = 'com.tencent.qqlive'#Package名字
 9 desired_caps['appActivity'] = '.ona.activity.WelcomeActivity'#appActivity名字
10 desired_caps['unicodeKeyboard'] = True
11 desired_caps['resetKeyboard'] = True
12 desired_caps['noReset'] = True
13 desired_caps['newCommandTimeout'] = 6000
14 
15 driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
16 driver.implicitly_wait(10)
17 
18 time.sleep(5)
19 
20 screenSize = driver.get_window_size()#返回个字典
21 print(f"当前屏幕尺寸为{screenSize}")#当前屏幕尺寸为{'width': 1080, 'height': 2280} 22 width = screenSize['width']
23 height = screenSize['height']
24 
25 for i in range(20):
26     driver.swipe(width * 0.2, height * 0.3, width * 0.8, height * 0.3, 500)#重点看这里
27     time.sleep(0.5)

 

3、经过location属性和size属性,计算得出滑动的起止坐标

ele = driver.find_element_by_xpath("//*[@resource-id='com.tencent.qqlive:id/c0b']//android.widget.RelativeLayout[2]")#先选择元素
location = ele.location#获取元素位置(左上角坐标){'x': 1050, 'y': 363}
size = ele.size#获取元素尺寸{'height': 567, 'width': -6}start_x = location["x"]+int(size['width'])*0.2start_y = location["y"]+int(size['height'])*0.5end_x = location['x']+int(size['width'])*0.8end_y = location['y']+int(size['height'])*0.5for i in range(20): driver.swipe(start_x,start_y,end_x,end_y,500) time.sleep(0.5)
相关文章
相关标签/搜索