Appium基础篇14-上下左右滑屏操作swipe方法

    本篇来学习webdriver.py下一个滑屏操作,这个方法叫swipe,这个方法只是手机端appium的方法,不是selenium3的方法。在介绍滑屏操作之前,你需要了解如何得到屏幕的高和宽,因为每个手机尺寸不一样。

1.先来看看x,y轴在appium中如何定义

2.左右上下滑屏代码实现

[python]  view plain  copy
  1. import os  
  2. import time  
  3. from appium import webdriver  
  4.   
  5. desired_caps ={ 'platformName''Android',  
  6.                 'platformVersion''6.0.1',  
  7.                 'deviceName''KIW-AL10',  
  8.                 'noReset'True,  
  9.                 'appPackage''com.baidu.searchbox',  
  10.                 'appActivity''com.baidu.searchbox.SplashActivity',  
  11.                 'unicodeKeyboard'True,  
  12.                 'resetKeyboard'True  
  13.                 }  
  14.   
  15. driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)#启动app  
  16. time.sleep(3#app启动后等待3秒,方便元素加载完成  
  17. # 打印屏幕高和宽  
  18. print(driver.get_window_size())  
  19. #获取屏幕的高  
  20. x = driver.get_window_size()['width']  
  21. # 获取屏幕宽  
  22. y = driver.get_window_size()['height']  
  23. # 滑屏,大概从屏幕右边2分之一高度,往左侧滑动,滑动后显示的是 热点tab  
  24. driver.swipe(6/7*x, 1/2*y, 1/7*x, 1/2*y, 100)  
  25. time.sleep(4)  
  26. #向右滑动,显示推荐tab 内容,第五个参数,时间设置大一点,否则容易看不到滑动效果  
  27. driver.swipe(1/7*x, 1/2*y, 5/7*x, 1/2*y, 200)  
  28. time.sleep(4)  
  29. #向上滑  
  30. driver.swipe(1/2*x, 1/2*y, 1/2*x, 1/7*y, 200)  
  31. time.sleep(4)  
  32. # 向下滑动  
  33. driver.swipe(1/2*x, 1/7*y, 1/2*x, 6/7*y, 200)  

      具体代码上面都写了备注,你需要根据上面图,去理解或者比划一下,滑屏开始点坐标和滑屏结束点坐标的位置,这些需要指出,swipe中第五个参数,如果缺省,默认是5毫秒,我建议设置100或者200毫秒,这样才方便观察滑屏,否则滑屏效果可能不到位。

猜你喜欢

转载自blog.csdn.net/qq_24857309/article/details/79664432