在appium 的appium\webdriver\extensions\action_helpers.py下提供了一個可以上下左右滑動的方法:swipe()
這個方法用起來到也比較簡單,首先獲取屏幕的寬度、高度,然后計算出滑動的開始位置到結束位置的距離,
再把參數傳遞給swipe()調用即可:
不過要先弄清楚手機屏幕的起始坐標位置,要不然,滑動時就打不到效果了。
手機屏幕的起始坐標都是在左上角,也就是說左上角開始位置x軸、y軸都是0。弄清楚這一點,接一下來就好做了。
如下圖
具體實現代碼如下:
#-*-encoding:utf-8-*- from appium import webdriver from time import sleep desired_caps = { "platformName":"Android", "platformVersion":"6.0", "deviceName":"PJQDU16715003110", # "appPackage":"com.tencent.mtt", # "appActivity":"com.tencent.mtt.MainActivity", # "appActivity":"com.android.chrome", # "appPackage":"com.tencent.mobileqq", # "appActivity":"com.tencent.mobileqq.activity.SplashActivity", "appPackage":"com.tencent.mm", "appActivity":".ui.LauncherUI", "automationName":"uiautomator2", "unicodeKeyboard":"True", "resetKeyboard":"True", "noReset":"True", "chromeOptions":{"androidProcess":"com.tencent.mm:tools"} # PJQDU16715003110 } driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps) sleep(5) #獲取手機屏幕寬、高 x = driver.get_window_size()["width"] y = driver.get_window_size()["height"] # print x,y def swipe_down(driver,start_y=0.25,stop_y=0.75,duration=3000): #按下手機屏幕,向下滑動 #注意,向下滑時,x軸不變,要不然就變成了斜向下滑動了 #@duration:持續時間 x1 = int(x*0.5) y1 = int(y*start_y) x2 = int(x*0.5) y2 = int(y*stop_y) # print x1,y1,x2,y2 driver.swipe(x1,y1,x2,y2,duration) def swipe_up(driver,start_y=0.75,stop_y=0.25,duration=3000): #按下手機屏幕,向上滑動 #注意,向上滑時,x軸不變,要不然就變成了斜向下滑動了 #@duration:持續時間 x1 = int(x*0.5) y1 = int(y*start_y) x2 = int(x*0.5) y2 = int(y*stop_y) # print x1,y1,x2,y2 driver.swipe(x1,y1,x2,y2,duration) def swipe_left(driver,star_x=0.75,stop_x=0.25,duration=3000): #按下手機屏幕,向左邊滑動 #注意,向左邊滑時,y軸不變 #@duration:持續時間 x1 = int(x*star_x) y1 = int(y*0.5) x2 = int(x*stop_x) y2 = int(y*0.5) # print x1,y1,x2,y2 driver.swipe(x1,y1,x2,y2,duration) def swipe_right(driver,star_x=0.25,stop_x=0.75,duration=3000): #按下手機屏幕,向右邊滑動 #注意,向左邊滑時,y軸不變 #@duration:持續時間 x1 = int(x*star_x) y1 = int(y*0.5) x2 = int(x*stop_x) y2 = int(y*0.5) # print x1,y1,x2,y2 driver.swipe(x1,y1,x2,y2,duration)