前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >appium实现app上、下、左、右滑动操作

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

原创
作者头像
用户8077380
发布2024-03-15 23:54:48
1280
发布2024-03-15 23:54:48

1. swipe方法语法 

代码语言:python
复制
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上、下、左、右滑动方法

代码语言:python
复制
# 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)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档