appium实现app上下左右滑动操作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了appium实现app上下左右滑动操作相关的知识,希望对你有一定的参考价值。

1. 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 - 持续时间,单位毫秒

2. 手机坐标:

手机从左上角开始为0,横着的是x轴,竖着的是y轴

3. 获取屏幕的size

size = driver.get_window_size()  

print (size)         # {\'width\': 1080, \'height\': 1920} ,返回字典类型,可使用字典的方法取width/height的值
print (size["width"]) # 获取浏览器的宽度1080
print (size["height"]) # 获取浏览器的高度1920

4. app上、下、左、右滑动方法

# coding:utf-8
from appium import webdriver
from time import sleep
desired_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.25
    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.25
    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实现app上下左右滑动操作的主要内容,如果未能解决你的问题,请参考以下文章

appium滑动

Appium-python-UI自动化之页面-上下滑动左右滑动swipe方法操作

appium--滑动

Python&Appium实现滑动引导页进入APP

appium---App页面滑动

Appium里面怎么实现 左右滑动屏幕