Python Appium 滑动、点击等操作

1、手机滑动-swip

# 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()

操作效果如下:

原文地址:https://www.cnblogs.com/moying-wq/p/10421074.html

时间: 2024-10-13 04:15:21

Python Appium 滑动、点击等操作的相关文章

python+appium模拟手机物理按键操作

一句代码:driver.keyevent()        括号里填入的是手机物理按键的数字代号 driver.press_keycode()        括号里填入的是键盘按键的数字代号 手机物理键数字代码 键盘按键数字代号 原文地址:https://www.cnblogs.com/xiaobaixiaobai/p/10760943.html

Python + Appium 环境搭建

---恢复内容开始--- Appium自动化公司内部测试培训1-环境搭建 课程目的 一.Python + Appium 环境搭建 课程内容 1    安装前准备工作 搭建环境所需要的安装文件已经下载好,从SVN上获取即可,解压,如下 2   Appium环境搭建 Appium是一个开源的,适用于原生或者移动网络和混合应用程序在 iOS 和 Android 平台上的的开源自动化测试框架. 2.1  安装Node.js 点击安装文件,下一步下一步直接安装,安装成功后,输入cmd打开windows的命

Python Appium 开启Android测试之路

1.获取 Android app的Activity 打开终端cmd,先cd进入到刚才下载的“新浪.apk”目录下,然后使用aapt dump badging xxx.apk命令获取包内信息.注意,启动类名称一个字母都不能错. aapt dump badging 新浪.apk cat>d:/log.txt     这样可以将信息指定到某一文件下然后 查看 2.启动微博 #-*-coding:utf-8-*- #Time:2017/7/20 18:06 #Author:YangYangJun imp

基于python+appium通过图片对比来做的UI自动化

1.python + appium +图片对比实现UI自动化:背景:当手机需要适配不同的语言时,测试过程中易出现因为语言不熟悉,导致UIbug被遗漏(例如setting中的描述性文字显示不完整等等问题)环境搭建:需使用模块PIL,Windows版本链接地址:http://pythonware.com/products/pil/ubuntu (使用16.04虚拟机):sudo apt-get install python-imaging安装过程遭遇Unable to acquire the dpk

python下selenium模拟浏览器基础操作

1.安装及下载 selenium安装: pip install selenium  即可自动安装selenium geckodriver下载:https://github.com/mozilla/geckodriver/releases Chromedriver下载:http://npm.taobao.org/mirrors/chromedriver/ 2.保存路径 将下载好的geckodriver以及Chromedriver解压到桌面,打开我的电脑,找到Python文件夹中anancode文件

Python+Appium实现自动化测试

一.环境准备 1.脚本语言:Python3.x    IDE:安装Pycharm 2.安装Java JDK .Android SDK 3.adb环境,path添加E:\Software\Android_SDK\platform-tools 4.安装Appium for windows,官网地址 http://appium.io/ 点击下载按钮会到GitHub的下载页面,选择对应平台下载 安装完成后,启动Appium,host和port默认的即可,然后设置Android SDk和Java JDK

python,使用PIL库对图片进行操作

在做识别验证码时,需要对验证码图片进行一些处理,所以就学习了一下PIL的知识,下面是我总结的一些常用方法. 注明:图片的操作都需要Image库,所以要使用import Image导入库 1.打开图片 import Imageimg=Image.open("code.jpg") 注:有些图片名称是包含中文的,就需要在""前加上u,例:img=Image.open(u"阿布.jpg") 以下各个操作均是基于原图进行的修改,原图为: 2.展示图片 im

获取appPackage和appActivity(python+appium)

(一般情况下,这两个参数可以让开发提供:如果有某种原因不能获取,而你只有安装包,那么可以通过以下步骤获取) 步骤一: 连接真机或开启安卓模拟器,打开Appium.(前提是python+appium环境是OK的) 步骤二: 在真机或模拟器上打开需要获取appPachage和appActivity的apk 步骤三: 开启日志输出(进入cmd,执行adb logcat>D:/log.txt)--路径自己随意哈 步骤四: 找到你刚输出的日志,打开日志,ctrl+f搜索下 Displayed,就能看到你需

如何实现Canvas图像的拖拽、点击等操作

上一篇Canvas的博文写完后,有位朋友希望能对Canvas绘制出来的图像进行点击.拖拽等操作,因为Canvas绘制出的图像能很好的美化.好像是想做炉石什么的游戏,我也没玩过. Canvas在我的理解中就好像在一张画布上绘制图像,它只能看到却“摸”不到,那要如何进行操作呢.我不知道网上是怎么做的,这里用自己的想法做了个DEMO分享给大家. 思路: 虽然Canvas不能拖拽,但div可以拖拽,那怎么把二者结合起来呢.初步想法是将一个与Canvas图像大小差不多的div覆盖在其上,在拖拽div时将获