lsekfe 发表于 2020-9-15 10:48:06

Appium滑动屏幕(2)——TouchAction

除了swipe(self, start_x, start_y, end_x, end_y, duration=None)函数支持模拟屏幕滑动外,具体操作可以参考文章Appium滑动屏幕(1)——swipe,我们还可以使用TouchAction(driver)类来实现,这个类提供了短按压press()方法,wait()方法,move_to()方法,release()方法,perform()方法等常用的方法。大致使用如下:
方法:press(element, x, y)
说明:表示按压的动作,element参数是一个元素对象,当element不为None时,x和y必须为None,如果element为None时,x如果不为None,那么y也不能为None。也就是说,在安卓操作系统中,element和(x,y)必须要传递一个,苹果系统可以不传。
方法:move_to(element, x, y)
说明:表示移动去哪里,element参数是一个元素对象,当element不为None时,x和y必须为None,如果element为None时,x如果不为None,那么y也不能为None。
方法:wait(duration)
说明:duration是时间,以毫秒为单位,这个方法的作用是等待一段时间,和sleep的作用类似,唯一区别是,sleep不能被TouchAtion对象访问。
方法:release()
说明:结合press等按压动作使用的,表示抬起动作。
方法:perform()
说明:使所有的按压press,等待wait,抬起release等动作生效。
以上滑操作为例,部分代码如下:
1、导入相应的包:
#coding=utf-8

from appium import webdriver

import time

from appium.webdriver.common.touch_action import TouchAction

from selenium.webdriver.support.wait import WebDriverWait
2、获取手机屏幕的大小:
def get_phone_size(driver):

    """获取手机屏幕的大小"""

    width = driver.get_window_size()['width']# 获取手机屏幕的宽

    height = driver.get_window_size()['height']# 获取手机屏幕的高

    return width, height
3、上滑操作:
def swipe_up(driver, duration=300):

    """上滑操作"""

    width, height = get_phone_size(driver)

    start_x , start_y = 1/2 * width ,3/4 * height

    end_x , end_y = 1/2 * width , 1/4 * height

TouchAction(driver).press(None, start_x, start_y).wait(duration).move_to(None, end_x, end_y).release().perform()#这句代码等价于滑动操作swipe
4、调用上滑函数来执行:
time.sleep(1)

swipe_up(driver)作者:静静地就好
链接:https://www.jianshu.com/p/28826d463c40
来源:简书




页: [1]
查看完整版本: Appium滑动屏幕(2)——TouchAction