python+Appium自動化:app滑動操作swipe


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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM