Appium swipe实现屏幕滑动

 在 Appium 中提供 swipe() 方法来模拟用户滑动屏幕。

 swipe() 实现过程 是先通过在屏幕上标记两个坐标,然后再从开始坐标移动到结束坐标。

 先看下 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`
        """

 start_x:开始坐标 x 轴

 start_y:开始坐标 y 轴

 end_x:结束坐标 x 轴

 end_y:结束坐标 y 轴

 duration:开始坐标移动到结束坐标的时间,默认 None

 

坐标原点位于屏幕左上角

Appium swipe实现屏幕滑动

 

 一段简单代码:

from appium import webdriver

desired_caps = {
                'platformName': 'Android',
                'deviceName': '192.168.41.101:5555',
                'platformVersion': '9.0',
                # apk包名
                'appPackage': 'com.gem.tastyfood',
                # apk的launcherActivity
                'appActivity': 'com.gem.tastyfood.LaunchActivity'
                }

if __name__=='__main__':
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    x = driver.get_window_size()["width"]
    y = driver.get_window_size()["height"]
    #向左滑动
    driver.swipe(x*0.9,y*0.5,x*0.1,y*0.5,duration=2000)

 

 可以将 左滑、右滑、上滑、下滑 写成方法,方便调用:

from appium import webdriver

desired_caps = {
                'platformName': 'Android',
                'deviceName': '192.168.41.101:5555',
                'platformVersion': '9.0',
                # apk包名
                'appPackage': 'com.gem.tastyfood',
                # apk的launcherActivity
                'appActivity': 'com.gem.tastyfood.LaunchActivity'
                }

# 向左滑动。y轴保持不变,X轴:由大变小
def swipe_left(driver,star_x=0.9,stop_x=0.1,duration=2000):
    x1 = int(x*star_x)
    y1 = int(y*0.5)
    x2 = int(x*stop_x)
    y2 = int(y*0.5)
    driver.swipe(x1,y1,x2,y2,duration)

# 向右滑动。y轴保持不变,X轴:由小变大
def swipe_right(driver,star_x=0.1,stop_x=0.9,duration=2000):
    x1 = int(x*star_x)
    y1 = int(y*0.5)
    x2 = int(x*stop_x)
    y2 = int(y*0.5)
    driver.swipe(x1,y1,x2,y2,duration)

# 向上滑动。x轴保持不变,y轴:由大变小
def swipe_up(driver,star_y=0.9,stop_y=0.1,duration=2000):
    x1 = int(x*0.5)
    y1 = int(y*star_y)
    x2 = int(x*0.5)
    y2 = int(y*stop_y)
    driver.swipe(x1,y1,x2,y2,duration)

# 向下滑动。x轴保持不变,y轴:由小变大
def swipe_down(driver,star_y=0.1,stop_y=0.9,duration=2000):
    x1 = int(x*0.5)
    y1 = int(y*star_y)
    x2 = int(x*0.5)
    y2 = int(y*stop_y)
    driver.swipe(x1,y1,x2,y2,duration)


if __name__=='__main__':
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    x = driver.get_window_size()["width"]
    y = driver.get_window_size()["height"]
    swipe_left(driver)
    swipe_right(driver)
    swipe_up(driver)
    swipe_down(driver)

 

 

 

上一篇:python爬虫(十五) 豆瓣电影爬虫


下一篇:js用策略思维做的小球滑动