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)
相關文章
相關標籤/搜索