1. 程式人生 > 實用技巧 >Appium-滑動操作

Appium-滑動操作

appium 提供給了 driver.swipe()方法進行頁面滑動操作

 1     def swipe(self: T, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0) -> T:
 2         """Swipe from one point to another point, for an optional duration.
 3 
 4         Args:
 5             start_x: x-coordinate at which to start
 6
start_y: y-coordinate at which to start 7 end_x: x-coordinate at which to stop 8 end_y: y-coordinate at which to stop 9 duration: time to take the swipe, in ms. 10 11 Usage: 12 driver.swipe(100, 100, 100, 400) 13 14 Returns: 15
Union['WebDriver', 'ActionHelpers']: Self instance座標

座標:X軸是從左到右的,Y軸是上從上到下的

  通過driver.get_windows_size 獲取螢幕解析度字典

  下滑操作:即從Y * 0.2的位置滑動到Y *0.8的位置,即 start_y = driver.get_windows.size().get('height') * 0.2 end_y = driver.get_windows.size().get('height') * 0.8

  其他滑動操作類似,記得座標即可

 1 from appium import
webdriver 2 3 desired_capabilities = { 4 "deviceName":"192.168.221.102:5555", #手機唯一ID 5 "platformVersion":"5.0", #手機版本 6 "platformName":"Android", # 裝置型別 7 "appPackage":"com.android.settings", #包名 'com.tencent.mobileqq' 8 "appActivity":".Settings", #入口 tv.danmaku.bili.ui.splash.SplashActivity 9 "noReset": True 10 } 11 12 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_capabilities) 13 14 size = driver.get_window_size() 15 width = size.get('width') 16 height = size.get('height') 17 print(size) 18 19 #居中上滑 20 start_x_end = width * 0.5 21 start_y = height * 0.8 22 end_y = height * 0.2 23 24 driver.swipe(start_x_end,start_y,start_x_end,end_y)