appium+python scroll、swipe滚动、滑动(上下左右)

scroll()方法是滑动页面,不过不是滑动滚动条,而是获取两个元素,然后从从一个元素滚动到另一个元素。

方法介绍:

scroll(self, origin_el, destination_el, duration=None):

参数:
          - originalEl - 要滚动的元素
          - destinationEl - 要滚动到的元素
          - dufrom appium import webdriver

从一个元素滚动到另一个元素

    def find_element(self, *loc):
        """
        重写find_element方法,显式等待
        """
        try:
            # self.driver.wait_activity(self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("%s")'), 10)
            # WebDriverWait(self.driver, 15).until(EC.visibility_of_element_located(loc))
            time.sleep(1)
            return self.driver.find_element(*loc)
        except NoSuchElementException as msg:
            print(u"查找元素异常: %s" % msg)
            # self.driver.back()
            # raise msg  # 抛出异常
            return False
    def scroll_page(self, loc1, loc2):
        stop_element = self.find_element(*loc2)
        start_element = self.find_element(*loc1)
        self.driver.scroll(start_element, stop_element, 3000)

 --------------------------------------------------

swipe()方法:

滑动屏幕

上下滑动

    def swipe_down_up(self, start_y=0.25, stop_y=0.75, duration=2000):  # 如果start_y=0.75, stop_y=0.25,则向上滑动屏幕
        # 按下手机屏幕,向下滑动
        # 注意,向下滑时,x轴不变,要不然就变成了斜向下滑动了
        # @duration:持续时间
        x = self.driver.get_window_size()["width"]  # 获取屏幕的宽
        y = self.driver.get_window_size()["height"]
        x1 = int(x * 0.5)
        y1 = int(y * start_y)
        x2 = int(x * 0.5)
        y2 = int(y * stop_y)
        self.driver.swipe(x1, y1, x2, y2, duration)

左右滑动

def swipe_left_right(self, start_x=0.75, stop_x=0.25, duration=2000):  # 如果start_x=0.25, stop_x=0.75,屏幕往右滑动
    # 按下手机屏幕,向左滑动
    # 注意,向上滑时,x轴不变,要不然就变成了斜向上滑动了
    # @duration:持续时间
    x = self.driver.get_window_size()["width"]  # 获取屏幕的宽
    y = self.driver.get_window_size()["height"]
    x1 = int(x * start_x)
    y1 = int(y * 0.5)
    x2 = int(x * stop_x)
    y2 = int(y * 0.5)
    self.driver.swipe(x1, y1, x2, y2, duration)

 滑动元素:

    def swipe_element_du(self, *loc, start_y=0.25, stop_y=0.75, duration=2000):  # 默认元素向下滑。如果start_y=0.75, stop_y=0.25,则向上滑动元素
        # 获取元素坐标
        x = self.x_y_w_h(*loc)['x']
        y = self.x_y_w_h(*loc)['y']
        # 获取元素宽、高
        width = self.x_y_w_h(*loc)['w']
        height = self.x_y_w_h(*loc)['h']
        x1 = int(x + width * 0.5)
        y1 = int(y + height * start_y)
        x2 = int(x + width * 0.5)
        y2 = int(y + height * stop_y)
        self.driver.swipe(x1, y1, x2, y2, duration)

x_y_w_h()为自己写的获取元素的坐标、宽、高:

    def x_y_w_h(self, *loc):  # 获取元素坐标,宽,高
        # 获取元素坐标
        ele = self.find_element(*loc).location
        ele_size = self.find_element(*loc).size
        list_x = {
            "x": ele.get('x'),
            "y": ele.get('y'),
            "w": ele_size['width'],
            "h": ele_size['height']
        }
        return list_x

scroll() 与swipe()的区别,swipe是可以根据自己需要设置滑动的距离,而scroll是根据页面中两个元素位置距离进行滑动。

原文地址:https://www.cnblogs.com/may18/p/14519322.html