1. 程式人生 > >Python Appium 滑動、點擊等操作

Python Appium 滑動、點擊等操作

arm sof 使用場景 坐標 args python art icu pyc

Python Appium 滑動、點擊等操作

1、手機滑動-swipe

# FileName : Tmall_App.py
# Author   : Adil
# DateTime : 2018/3/25 17:22
# SoftWare : PyCharm

from appium import  webdriver

caps = {}

caps[platformName] = Android
caps[platformVersion] = 6.0
caps[deviceName] = N79SIV5PVCSODAQC
caps[appPackage
] = com.tmall.wireless caps[appActivity] = com.tmall.wireless.splash.TMSplashActivity #隱藏鍵盤 caps[unicodeKeyboard] = True caps[resetKeyboard] = True driver = webdriver.Remote(http://127.0.0.1:4723/wd/hub, caps) driver.swipe() if __name__ == __main__: pass

查看源碼

Ctrl + 鼠標右鍵點擊 driver.swipe()

# convenience method added to Appium (NOT Selenium 3)
    def swipe(self, start_x, start_y, end_x, end_y, duration=None):
        """Swipe from one point to another point, for an optional duration.

        :Args:
         - start_x - x-coordinate at which to start
         - start_y - y-coordinate at which to start
         - end_x - x-coordinate at which to stop
         - end_y - y-coordinate at which to stop
         - duration - (optional) time to take the swipe, in ms.

        :Usage:
            driver.swipe(100, 100, 100, 400)
        
""" # `swipe` is something like press-wait-move_to-release, which the server # will translate into the correct action action = TouchAction(self) action .press(x=start_x, y=start_y) .wait(ms=duration) .move_to(x=end_x, y=end_y) .release() action.perform() return self

查看源碼語法,起點和終點四個坐標參數。 手機屏幕從左上角開始為0,向右為x軸坐標,向下為y軸坐標。

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)

向下滑動實例

# FileName : Tmall_App.py
# Author   : Adil
# DateTime : 2018/3/25 17:22
# SoftWare : PyCharm
import time
from appium import  webdriver

caps = {}

caps[platformName] = Android
caps[platformVersion] = 6.0
caps[deviceName] = N79SIV5PVCSODAQC
caps[appPackage] = com.tmall.wireless
caps[appActivity] = com.tmall.wireless.splash.TMSplashActivity
#隱藏鍵盤
caps[unicodeKeyboard] = True
caps[resetKeyboard] = True
driver = webdriver.Remote(http://127.0.0.1:4723/wd/hub, caps)

# 獲取屏幕的size
size = driver.get_window_size()
print(size)
# 獲取屏幕寬度 width
width = size[width]
print(width)
# 獲取屏幕高度 height
height = size[height]
print(height)

# 執行滑屏操作,向下(下拉)滑動
x1 = width*0.5
y1 = height*0.25
y2 = height*0.9
time.sleep(3)
print("滑動前")
driver.swipe(x1,y1,x1,y2)
print("滑動後")
# 增加滑動次數,滑動效果不明顯,增加滑動次數

for i in range(5):
    print("第%d次滑屏"%i)
    time.sleep(3)
    driver.swipe(x1,y1,x1,y2)
time.sleep(3)

driver.quit()



if __name__ == __main__:

    pass

封裝滑動方法,代碼如下:

# FileName : Tmall_App.py
# Author   : Adil
# DateTime : 2018/3/25 17:22
# SoftWare : PyCharm
import time
from appium import  webdriver

caps = {}

caps[platformName] = Android
caps[platformVersion] = 6.0
caps[deviceName] = N79SIV5PVCSODAQC
caps[appPackage] = com.tmall.wireless
caps[appActivity] = com.tmall.wireless.splash.TMSplashActivity
#隱藏鍵盤
caps[unicodeKeyboard] = True
caps[resetKeyboard] = True
driver = webdriver.Remote(http://127.0.0.1:4723/wd/hub, caps)

# 獲取屏幕的size
size = driver.get_window_size()
print(size)
# 獲取屏幕寬度 width
width = size[width]
print(width)
# 獲取屏幕高度 height
height = size[height]
print(height)

# 執行滑屏操作,向下(下拉)滑動
x1 = width*0.5
y1 = height*0.25
y2 = height*0.8
time.sleep(3)
print("滑動前")
driver.swipe(x1,y1,x1,y2)
print("滑動後")
# 增加滑動次數,滑動效果不明顯,增加滑動次數

for i in range(5):
    print("第%d次滑屏"%i)
    time.sleep(3)
    driver.swipe(x1,y1,x1,y2)
time.sleep(3)

# 封裝滑動方法

def swipeUp(driver,n = 5):
    ‘‘‘定義向上滑動方法‘‘‘
    print("定義向上滑動方法")
    x1 = width*0.5
    y1 = height*0.9
    y2 = height*0.25
    time.sleep(3)
    print("滑動前")
    for i in range(n):
        print("第%d次滑屏" % i)
        time.sleep(3)
        driver.swipe(x1, y1, x1, y2)


def swipeDown(driver,n = 5):
    ‘‘‘定義向下滑動方法‘‘‘
    print("定義向下滑動方法")
    x1 = width*0.5
    y1 = height*0.25
    y2 = height*0.9
    time.sleep(3)
    print("滑動前")
    for i in range(n):
        print("第%d次滑屏" % i)
        time.sleep(3)
        driver.swipe(x1, y1, x1, y2)

def swipeLeft(driver,n = 5):
    ‘‘‘定義向左滑動方法‘‘‘
    print("定義向左滑動方法")
    x1 = width*0.8
    x2 = width*0.2
    y1 = height*0.5

    time.sleep(3)
    print("滑動前")
    for i in range(n):
        print("第%d次滑屏" % i)
        time.sleep(3)
        driver.swipe(x1, y1, x2, y1)

def swipeRight(driver,n = 5):
    ‘‘‘定義向右滑動方法‘‘‘
    print("定義向右滑動方法")
    x1 = width*0.2
    x2 = width*0.8
    y1 = height*0.5

    time.sleep(3)
    print("滑動前")
    for i in range(n):
        print("第%d次滑屏" % i)
        time.sleep(3)
        driver.swipe(x1, y1, x2, y1)

if __name__ == __main__:

    swipeUp(driver)
    swipeDown(driver)
    swipeLeft(driver)
    swipeRight(driver)

    driver.quit()

2、點擊手機屏幕坐標-tap

使用場景:有時候定位元素的時候,你使出了十八班武藝還是定位不到,怎麽辦呢?(面試經常會問)
那就拿出絕招:點元素所在位置的坐標

import time
from appium import  webdriver

caps = {}

caps[platformName] = Android
caps[platformVersion] = 6.0
caps[deviceName] = N79SIV5PVCSODAQC
caps[appPackage] = com.tmall.wireless
caps[appActivity] = com.tmall.wireless.splash.TMSplashActivity
#隱藏鍵盤
caps[unicodeKeyboard] = True
caps[resetKeyboard] = True
driver = webdriver.Remote(http://127.0.0.1:4723/wd/hub, caps)

driver.tap()

查看源碼

Ctrl + 鼠標右鍵點擊 driver.tap()

# convenience method added to Appium (NOT Selenium 3)
    def tap(self, positions, duration=None):
        """Taps on an particular place with up to five fingers, holding for a
        certain time

        :Args:
         - positions - an array of tuples representing the x/y coordinates of
         the fingers to tap. Length can be up to five.
         - duration - (optional) length of time to tap, in ms

        :Usage:
            driver.tap([(100, 20), (100, 60), (100, 100)], 500)
        """
        if len(positions) == 1:
            action = TouchAction(self)
            x = positions[0][0]
            y = positions[0][1]
            if duration:
                action.long_press(x=x, y=y, duration=duration).release()
            else:
                action.tap(x=x, y=y)
            action.perform()
        else:
            ma = MultiAction(self)
            for position in positions:
                x = position[0]
                y = position[1]
                action = TouchAction(self)
                if duration:
                    action.long_press(x=x, y=y, duration=duration).release()
                else:
                    action.press(x=x, y=y).release()
                ma.add(action)

            ma.perform()
        return self

tap是模擬手指點擊,一般頁面上元素
的語法有兩個參數,第一個是positions,是list類型最多五個點,duration是持續時間,單位毫秒

tap(self, positions, duration=None):

    Taps on an particular place with up to five fingers, holding for a certain time
    
    模擬手指點擊(最多五個手指),可設置按住時間長度(毫秒)
    
    :Args:
    
    - positions - list類型,裏面對象是元組,最多五個。如:[(100, 20), (100, 60)]
    
    - duration - 持續時間,單位毫秒,如:500
    
    :Usage:
    
    driver.tap([(100, 20), (100, 60), (100, 100)], 500)

實際應用:坐標定位

如圖:查看元素坐標,可以看到右側bonds屬性

技術分享圖片

代碼實例如下:

# FileName : Tamll_App_TapDemo.py
# Author   : Adil
# DateTime : 2018/3/26 17:44
# SoftWare : PyCharm

import time
from appium import  webdriver

caps = {}

caps[platformName] = Android
caps[platformVersion] = 6.0
caps[deviceName] = N79SIV5PVCSODAQC
caps[appPackage] = com.tmall.wireless
caps[appActivity] = com.tmall.wireless.splash.TMSplashActivity
#隱藏鍵盤
caps[unicodeKeyboard] = True
caps[resetKeyboard] = True
driver = webdriver.Remote(http://127.0.0.1:4723/wd/hub, caps)


# 操作元素坐標點擊
# 天貓-天貓超市 坐標
def tapHit(driver):
    time.sleep(3)
    driver.tap([(234,324),(438,561)],500)
    time.sleep(2)


if __name__ == __main__:
    tapHit(driver)

    time.sleep(15)
    driver.quit()

操作效果如下:

技術分享圖片

說明:

通過坐標定位是元素定位的下下下策,實在沒辦法才用這個,另外如果換了手機分辨率,這個坐標就不能寫死了,得算出所在屏幕的比例。

Python Appium 滑動、點擊等操作