Python Appium 滑动、点击等操作

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

 

操作效果如下:

 

 

说明:

 通过坐标定位是元素定位的下下下策,实在没办法才用这个,另外如果换了手机分辨率,这个坐标就不能写死了,得算出所在屏幕的比例。

 

转载于:https://www.cnblogs.com/BlueSkyyj/p/8651365.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中使用Appium进行滑动屏幕操作可以通过以下几种方式实现: 1. 使用driver.swipe()方法进行滑动。例如,可以使用以下代码将屏幕从右向左滑动: driver.swipe(width*0.9, height*0.5, width*0.1, height*0.5, 1000) \[1\] 2. 将滑动屏幕的操作封装成方法,方便后续调用。可以创建一个基础操作类,例如BaseOpera,其中包含了滑动屏幕的各个方向的方法。以下是一个示例代码: class BaseOpera(object): def __init__(self, driver): self.driver = driver self.duration = 1000 @property def width(self): return self.driver.get_window_size()\['width'\] @property def height(self): return self.driver.get_window_size()\['height'\] def swipe_to_left(self, base=0.1): return self.driver.swipe(self.width*(1-base), self.height*0.5, self.width*base, self.height*0.5, self.duration) def swipe_to_right(self, base=0.1): return self.driver.swipe(self.width*base, self.height*0.5, self.width*(1-base), self.height*0.5, self.duration) def swipe_to_top(self, base=0.9): return self.driver.swipe(self.width*0.5, self.height*base, self.width*0.5, self.height*(1-base), self.duration) def swipe_to_bottom(self, base=0.9): return self.driver.swipe(self.width*0.5, self.height*(1-base), self.width*0.5, self.height*base, self.duration) \[3\] 3. 使用TouchAction类进行滑动操作。可以使用以下代码将元素A在X轴上滑动: from appium.webdriver.common.touch_action import TouchAction swipElement = driver.find_element_by_id('XXX') point = swipElement.location pointX = point\['x'\] + driver.get_window_size()\['width'\]*0.7 pointY = point\['y'\] TouchAction(driver).long_press(swipElement).move_to(x=pointX, y=pointY).release().perform() \[2\] 以上是几种常用的在Python中使用Appium进行滑动屏幕操作的方法。根据具体的需求和场景,可以选择适合的方法进行操作。 #### 引用[.reference_title] - *1* *3* [Python+Appium自动化之swipe()滑动页面](https://blog.csdn.net/hai_XG/article/details/118108724)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python-appium滑动Android屏幕的几种方式汇总](https://blog.csdn.net/jianglianye21/article/details/90070718)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值