swipe介绍
swipe函数可用于滑动屏幕,参数包括起点、终点坐标、滑动屏幕的持续时间。函数原型如下:
swipe(start_x, start_y, end_x, end_y, duration=None)
注意:手机从左上角开始为0.0 , 横着的是x轴,竖着的是y轴。
思路:先获取手机屏幕的宽和高,再自定义比例系数。
实践操作:
打开“小猿搜题app”,点击跳转至“猿辅导”页面,再执行向上、向下、向左、向右滑动。
# SwipeObj.py class SwipeMethod(): '''封装包含向上、向下、向左、向右滑动的对象''' def __init__(self, driver): self.driver = driver self.size = self.driver.get_window_size() # 获取当前window的 width 和 height print("width", self.size['width']) print("height", self.size['height']) def swipeUp(self, t=500, n=1): '''封装向上滑动的函数''' x1 = self.size['width'] * 0.5 # 设置x1起始和终点坐标,系数0.5可自适应设置 y1 = self.size['height'] * 0.75 # 设置y1起始坐标,系数0.75可自适应设置 y2 = self.size['height'] * 0.2 # 设置y2终点坐标,系数0.2可自适应设置 for i in range(n): self.driver.swipe(x1, y1, x1, y2, t) def swipeDown(self, t=500, n=1): '''封装向下滑动的函数''' x1 = self.size['width'] * 0.5 # 设置x1起始和终点坐标,系数可自适应设置 y1 = self.size['height'] * 0.25 # 设置y1起始坐标,系数可自适应设置 y2 = self.size['height'] * 0.7 # 设置y2终点坐标,系数可自适应设置 for i in range(n): self.driver.swipe(x1, y1, x1, y2, t) def swipeLeft(self, t=500, n=1): '''封装向左滑动的函数''' x1 = self.size['width'] * 0.75 # 设置x1起始坐标,系数可自适应设置 x2 = self.size['width'] * 0.2 # 设置x2终点坐标,系数可自适应设置 y1 = self.size['height'] * 0.5 # 设置y1起始和终点坐标,系数可自适应设置 for i in range(n): self.driver.swipe(x1, y1, x2, y1, t) def swipeRight(self, t=500, n=1): '''封装向右滑动的函数''' x1 = self.size['width'] * 0.2 # 设置x1起始坐标,系数可自适应设置 x2 = self.size['width'] * 0.75 # 设置x2终点坐标,系数可自适应设置 y1 = self.size['height'] * 0.5 # 设置y1起始和终点坐标,系数可自适应设置 for i in range(n): self.driver.swipe(x1, y1, x2, y1, t) if __name__ == "__main__": pass
# startApp.py from appium import webdriver from SwipeObj import SwipeMethod from time import sleep desired_caps = { 'autoLaunch': 'False', 'deviceName': 'honor10', 'platformName': 'Android', 'platformVersion': '10.0', 'appPackage': 'com.fenbi.android.solar', 'appActivity': 'com.fenbi.android.solar.activity.RouterActivity', 'noReset': 'True', } driver = webdriver.Remote('127.0.0.1:4723/wd/hub', desired_caps) class SwipeTest(): '''初始化对象''' def __init__(self, driver): self.driver = driver self.driver.launch_app() # 设置启动app self.swipe = SwipeMethod(self.driver) def swipeTest(self): self.driver.find_element_by_id("com.fenbi.android.solar:id/tutor_tab_text").click() # 点击跳转至【猿辅导】 sleep(2) self.swipe.swipeUp() sleep(3) self.swipe.swipeDown() sleep(3) self.swipe.swipeLeft() sleep(3) self.swipe.swipeRight() sleep(3) if __name__ =="__main__": swipe = SwipeTest(driver) swipe.swipeTest()
参考:https://www.cnblogs.com/yoyoketang/p/7766878.html