Appium-Python3--UI自动化-[-4-]-页面-上下滑动、左右滑动swipe方法操作
1.首先看app中怎么划分横纵坐标
2.swipe函数
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 (int): x-coordinate at which to start start_y (int): y-coordinate at which to start end_x (int): x-coordinate at which to stop end_y (int): y-coordinate at which to stop duration (:obj:`int`, optional): time to take the swipe, in ms. Usage: driver.swipe(100, 100, 100, 400) Returns: `appium.webdriver.webelement.WebElement` """ # `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
解析:
swipe(self, start_x, start_y, end_x, end_y, duration=None)
swipe(开始横坐标,开始纵坐标,结束横坐标,结束纵坐标,持续的时间,单位毫秒)
重点:在执行swipe之前一定要强制sleep 3-4s,不然会出现:wipe did not complete successfully报错
3.各种滑动
3.1 因为每个手机的坐标可能都不一样,这里我们可以通过先获取手机屏幕的长和宽,然后再次计算需要滑动的坐标位置,
def get_myWindow_size(self): ''' 获取手机长宽 :return: ''' x = self.driver.get_window_size()['width'] # 获取x轴的长度 y = self.driver.get_window_size()['height'] # 获取y轴的长度 return x,y
3.2 向下滑动,即x轴不变,y轴减小
def swipeDown(self): ''' 页面向下滑动 :return: ''' size = self.get_myWindow_size() MyLog.logger().info("size :" +str(size[0])+','+str(size[1])) x1 = int(size[0] * 0.5) # size[0]取元组的第一个值,*0.5表示中间的点 y1 = int(size[1] * 3/4) # size[1]取元组的第二个值,*0.1表示距离底部近 y2 = int(size[1] * 1/6) time.sleep(4) self.driver.swipe(x1, y1, x1, y2, 1000)
3.3 向下滑动,即x轴不变,y轴增大
def swipeUp(self): ''' 页面向上滑动 :return: ''' size = self.get_myWindow_size() MyLog.logger().info("size :" +str(size[0])+','+str(size[1])) x1 = int(size[0] * 0.5) # size[0]取元组的第一个值,*0.5表示中间的点 y1 = int(size[1] * 1/6) # size[1]取元组的第二个值,*0.1表示距离底部近 y2 = int(size[1] * 3/4) time.sleep(4) self.driver.swipe(x1, y1, x1, y2, 1000)
3.4 向右滑动 ,即x减小,y轴不变
def swipRight(self): ''' 页面向右滑动 :return: ''' size = self.get_myWindow_size() MyLog.logger().info("size :" +str(size[0])+','+str(size[1])) x1 = int(size[0] * 0.5) # size[0]取元组的第一个值,*0.5表示中间的点
x2 = int(size[0] * 0.25) # size[0]取元组的第一个值,*0.5表示中间的点
y1 = int(size[1] * 3/4) # size[1]取元组的第二个值,*0.1表示距离底部近
y2 = int(size[1] * 1/6)
time.sleep(4) self.driver.swipe(x1, y1, x2, y1, 1000)
3.5 向左滑动,即x轴增大,y轴不变
def swipLeft(self): ''' 页面向左滑动 :return: ''' size = self.get_myWindow_size() MyLog.logger().info("size :" +str(size[0])+','+str(size[1])) x1 = int(size[0] * 0.25) # size[0]取元组的第一个值,*0.5表示中间的点 x2 = int(size[0] * 0.75) # size[0]取元组的第一个值,*0.5表示中间的点 y1 = int(size[1] * 3/4) # size[1]取元组的第二个值,*0.1表示距离底部近 y2 = int(size[1] * 1/6) time.sleep(4) self.driver.swipe(x1, y1, x2, y1, 1000)
当有些人一出生就有的东西,我们要为之奋斗几十年才拥有。但有一样东西,你一辈子都不会有,那就是我们曾经一无所有。