在 Appium 中提供 swipe() 方法來模擬用戶滑動屏幕。web
swipe() 實現過程 是先經過在屏幕上標記兩個座標,而後再從開始座標移動到結束座標。app
先看下 swipe 方法定義:spa
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 軸code
start_y:開始座標 y 軸orm
end_x:結束座標 x 軸blog
end_y:結束座標 y 軸ip
duration:開始座標移動到結束座標的時間,默認 Noneelement
座標原點位於屏幕左上角get
一段簡單代碼:it
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)