Appium 的scroll()方法也是滑动页面,不过不是滑动滚动条,而是获取两个元素,然后从从一个元素滚动到另一个元素。
方法介绍:
scroll(self, origin_el, destination_el, duration=None):
参数:
- originalEl - 要滚动的元素
- destinationEl - 要滚动到的元素
- dufrom appium import webdriver
举个例子
#-*-encoding:utf-8-*-
from appium import webdriver
from time import sleep
desired_caps = {
"platformName":"Android",
"platformVersion":"6.0",
"deviceName":"PJQDU16715003110",
"appPackage":"com.tencent.mm",
"appActivity":".ui.LauncherUI",
"automationName":"uiautomator2",
"unicodeKeyboard":"True",
"resetKeyboard":"True",
"noReset":"True",
"chromeOptions":{"androidProcess":"com.tencent.mm:tools"}
}
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
sleep(15)
#获取手机屏幕宽、高
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)
sleep(3)
driver.find_element_by_xpath("//android.widget.FrameLayout[@index='0' and @resource-id='com.tencent.mm:id/xr' and @class='android.widget.FrameLayout' ]").click()
def scroll_page():
#获取【图书】、【好物享实惠】两个元素,将页面向上滑动
sleep(5)
stop_element=driver.find_element_by_name("图书")
start_element=driver.find_element_by_name("好物享实惠")
driver.scroll(start_element,stop_element,3000)
scroll() 与swipe()的区别,swipe是可以根据自己需要设置滑动的距离,而scroll是根据页面中两个元素位置距离进行滑动。