標籤:預設 tle pack webdriver rem tao one 次數 hub
swipe介紹
1.查看源碼文法,起點和終點四個座標參數,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)
2.手機從左上方開始為0,橫著的是x軸,豎著的是y軸
擷取座標
1.由於每個手機螢幕的解析度不一樣,所以同一個元素在不同手機上的座標也是不一樣的,滑動的時候座標不能寫死了。可以先擷取螢幕的寬和高,再通過比例去計算。
# coding:utf-8from appium import webdriverdesired_caps = { ‘platformName‘: ‘Android‘, ‘deviceName‘: ‘30d4e606‘, ‘platformVersion‘: ‘4.4.2‘, # apk包名 ‘appPackage‘: ‘com.taobao.taobao‘, # apk的launcherActivity ‘appActivity‘: ‘com.taobao.tao.welcome.Welcome‘ }driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps)# 擷取螢幕的sizesize = driver.get_window_size()print(size)# 螢幕寬度widthprint(size[‘width‘])# 螢幕高度widthprint(size[‘height‘])
2.運行結果:
{u‘width‘: 720, u‘height‘: 1280}7201280
封裝滑動方法
1.把上下左右四種常用的滑動方法封裝,這樣以後想滑動螢幕時候就能直接調用了
參數1:driver
參數2:t是期間
參數3:滑動次數
2.案例參考
# coding:utf-8from appium import webdriverfrom time import sleepdesired_caps = { ‘platformName‘: ‘Android‘, ‘deviceName‘: ‘30d4e606‘, ‘platformVersion‘: ‘4.4.2‘, # apk包名 ‘appPackage‘: ‘com.taobao.taobao‘, # apk的launcherActivity ‘appActivity‘: ‘com.taobao.tao.welcome.Welcome‘ }driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub‘, desired_caps)def swipeUp(driver, t=500, n=1): ‘‘‘向上滑動螢幕‘‘‘ l = driver.get_window_size() x1 = l[‘width‘] * 0.5 # x座標 y1 = l[‘height‘] * 0.75 # 起始y座標 y2 = l[‘height‘] * 0.25 # 終點y座標 for i in range(n): driver.swipe(x1, y1, x1, y2, t)def swipeDown(driver, t=500, n=1): ‘‘‘向下滑動螢幕‘‘‘ l = driver.get_window_size() x1 = l[‘width‘] * 0.5 # x座標 y1 = l[‘height‘] * 0.25 # 起始y座標 y2 = l[‘height‘] * 0.75 # 終點y座標 for i in range(n): driver.swipe(x1, y1, x1, y2,t)def swipLeft(driver, t=500, n=1): ‘‘‘向左滑動螢幕‘‘‘ l = driver.get_window_size() x1 = l[‘width‘] * 0.75 y1 = l[‘height‘] * 0.5 x2 = l[‘width‘] * 0.05 for i in range(n): driver.swipe(x1, y1, x2, y1, t)def swipRight(driver, t=500, n=1): ‘‘‘向右滑動螢幕‘‘‘ l = driver.get_window_size() x1 = l[‘width‘] * 0.05 y1 = l[‘height‘] * 0.5 x2 = l[‘width‘] * 0.75 for i in range(n): driver.swipe(x1, y1, x2, y1, t)if __name__ == "__main__": print(driver.get_window_size()) sleep(5) swipLeft(driver, n=2) sleep(2) swipRight(driver, n=2)
appium+python自動化24-滑動方法封裝(swipe)【轉載】