Appium 滑动

963 阅读5分钟

1.scroll事件

概念

从一个元素滑动到另一个元素,直到页面自动停止

ps:必须要求两个元素在界面上可见,否则会报错

# orgin_el:滑动开始的元素
# destination_el:滑动结束的元素
# duration滑动持续的时间,默认600ms,时间越大滑动越慢
driver.scroll(orgin_el,destination_el,duration=600)

2.drag_and_drop事件

概念

从一个元素滑动到另一个元素,第二个元素替代第一个元素原本在屏幕上的位置

ps:和scroll一样同样要求两个元素在界面可见,否则报错 于scroll不同的是不能设置持续时间,但滑动效果比scroll更精确,几乎没有惯性滑动

# orgin_el:滑动开始的元素
# destination_el:滑动结束的元素
driver.drag_and_drop(orgin_el,destination_el)

3.swipe事件

概念

从一个坐标点滑动界面到另一个坐标点,常用于开屏广告多图片连续滑动

# x1,y1:第一个点的x和y坐标
# x2,y2:第二个点的x和y坐标
# duration滑动持续的时间,时间越大滑动越慢
driver.swipe(x1,y1,x2,y2,duration)

实例一:常用的上下左右滑动


# 获取手机屏幕尺寸
def get_size(self):
    x = self.driver.get_window_size()['width']
    y = self.driver.get_window_size()['height']
    return (x, y)

# 屏幕向上滑动(从中间位置,由下往上滑,比例可以自己调节)
def swipeUp(self):
    l = self.get_size()
    x1 = int(l[0] * 0.5)  # x坐标
    y1 = int(l[1] * 0.5)  # 起始y坐标
    y2 = int(l[1] * 0.45)  # 终点y坐标
    self.driver.swipe(x1, y1, x1, y2, 200)
# 屏幕向下滑动(从中间位置,由上往下滑,比例可以自己调节)
def swipeDown(self):
    l = self.get_size()
    x1 = int(l[0] * 0.5)  # x坐标
    y1 = int(l[1] * 0.45)  # 起始y坐标
    y2 = int(l[1] * 0.5)  # 终点y坐标
    self.driver.swipe(x1, y1, x1, y2, 200)
# 屏幕向左滑动(从中间位置,由右往左滑,比例可以自己调节)
def swipeLeft(self):
    l = self.get_size()
    x1 = int(l[0] * 0.9)  # 起始x坐标
    x2 = int(l[0]*0.1)  # 终点x坐标
    y1 = int(l[1] * 0.5)  # y坐标
    self.driver.swipe(x1, y1, x2, y1, 1000)
# 屏幕向右滑动(从中间位置,由左往右滑,比例可以自己调节)
def swipeLeft(self):
    l = self.get_size()
    x1 = int(l[0] * 0.1)  # 起始x坐标
    x2 = int(l[0]*0.9)  # 终点x坐标
    y1 = int(l[1] * 0.5)  # y坐标
    self.driver.swipe(x1, y1, x2, y1, 1000)

实例二:日期控件的滑动

image.png


def select_date(year, month, day):
    # 假设cur_year,cur_month,cur_day为当前控件显示的年月日
    # 通过与想要滑动的年月日进行比较大小来决定往上滑还是往下滑
    while True:
        cur_year = b.find_element(*year_wv).get_attribute(name='content-desc')
        cur_year = int(cur_year[:-1])
        if year == cur_year:
            break
        if year > cur_year:
            driver.swipe(143, 1983, 143, 1883, duration=1000)
        else:
            driver.swipe(143, 1883, 143, 1983, duration=1000)
    while True:
        cur_month = b.find_element(*month_wv).get_attribute(name='content-desc')
        cur_month = int(cur_month[:-1])
        if month == cur_month:
            break
        if month > cur_month:
            driver.swipe(409, 1983, 409, 1883, duration=1000)
        else:
            driver.swipe(409, 1883, 409, 1983, duration=1000)
    while True:
        cur_day = b.find_element(*day_wv).get_attribute(name='content-desc')
        cur_day = int(cur_day[:-3])
        if day == cur_day:
            break
        if day > cur_day:
            driver.swipe(640, 1983, 640, 1883, duration=1000)
        else:
            driver.swipe(640, 1883, 640, 1983, duration=1000)

# 页面元素

year_wv = ['id', "com.mymoney:id/year_wv"] 
month_wv = ['id', 'com.mymoney:id/month_wv']
day_wv = ['id', 'com.mymoney:id/day_wv']
hour_of_day_wv = ['id', "com.mymoney:id/hour_of_day_wv"]
minute_wv = ['id', 'com.mymoney:id/minute_wv']

select_date(year, month, day)

4.TouchAction事件

概念

TouchAction可以实现手势操作,比如滑动、长按、拖动等,可以将这些基本手势组合成一个相对复杂的手势,最常见的是手势解锁密码

常见手势

  1. 轻敲手势(tap)

tap可以接受一个坐标值作为敲击的区域,而click只能接受对象作为参数,另外tap可以设置count,表示敲击次数,如果双击就是count=2

form appium.webdriver.common.touch_action import TouchAction
# 构造touchaction对象
ta =TouchAction(driver)
ta.tap(element=None,x=None,y=None,count=1).perform()

2. 长按手势(按住press/长按long_press操作)

form appium.webdriver.common.touch_action import TouchAction
# 构造touchaction对象
ta =TouchAction(driver)
#元素对象和坐标两种方式二选1
#element:需要长按的元素
#x:长按的x坐标
#y:长按的y坐标
#duration:长按时间,毫秒
ta.long_press(element=None,x=None,y=None,duration=1000).perform()

# 长按元素,持续 3 秒  
action.long_press(element, duration=3000).release().perform()  
#`long_press` 方法接受两个参数:要长按的元素和长按的持续时间(以毫秒为单位)。然后,通过调用 `perform` 方法来执行这个长按操作

3. 手指移动操作(moveTo)

form appium.webdriver.common.touch_action import TouchAction
# 构造touchaction对象
ta =TouchAction(driver)
#元素对象和坐标两种方式二选1
#element:需要长按的元素
#x:长按的x坐标
#y:长按的y坐标
#duration:长按时间,毫秒
ta.moveTo(element=None,x=None,y=None,duration=1000).perform()

将指针(光标)从过去指向指定的元素或点。

使用步骤

  1. 创建TouchAction对象
  2. 通过对象调用想执行的手势
  3. 通过perform方法执行动作(所有的手势必须通过perform方法来触发)

实例(手势密码)

详见前面的文章(Appium 实现设置手势密码):juejin.cn/post/684490… image.png

#这里准备绘制的路径为(index)0-3-6-7-8
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
list_pwd = driver.find_elements_by_class_name("android.view.View")
#第一次绘制手势密码
TouchAction(driver).press(list_pwd[1]).move_to(list_pwd[1]).move_to(list_pwd[4]).wait(100).move_to(
    list_pwd[7]).wait(100).move_to(list_pwd[8]).wait(100).move_to(list_pwd[9]).release().perform()
#第二次绘制手势密码
try:
    loc = ("xpath", "//*[@text='请再次绘制解锁图案']")
    e = WebDriverWait(driver, 1, 0.5).until(EC.presence_of_element_located(loc))
    TouchAction(driver).press(list_pwd[1]).move_to(list_pwd[1]).move_to(list_pwd[4]).wait(100).move_to(
        list_pwd[7]).wait(100).move_to(list_pwd[8]).wait(100).move_to(list_pwd[9]).release().perform()
except Exception as e:
    print("[Error]: ", e)