python-appium滑动Android屏幕的几种方式汇总

1、swip:通过坐标控制屏幕的滚动

def swipe(self, start_x, start_y, end_x, end_y, duration=None)

封装使用方法:

# 获得机器屏幕大小x,y
def getSize(driver):
    x = driver.get_window_size()['width']
    y = driver.get_window_size()['height']
    return (x, y)

# 屏幕上下滑动
def swipeDownUp(driver,x,y1,y2,t):
    s = getSize(driver)
    x1 = int(s[0] * x)  # x坐标
    y1 = int(s[1] * y1)  # 起始y坐标
    y2 = int(s[1] * y2)  # 终点y坐标
    driver.swipe(x1, y1, x1, y2, t)


# 屏幕左右滑动
def swipRightLeft(driver,x1,x2,y1,t):
    s = getSize(driver)
    x1 = int(s[0] * x1)
    y1 = int(s[1] * y1)
    x2 = int(s[0] * x2)
    driver.swipe(x1, y1, x2, y1, t)

2、scroll:屏幕从一个元素滑动到另一个元素

def scroll(self, origin_el, destination_el, duration=None)

示例方法:

testButtons = driver.find_elements_by_id('XXX')
driver.scroll(testButtons[3],testButtons[1])

3、控制一个元素滑动到某个位置

控制元素A在X轴上滑动:

from appium.webdriver.common.touch_action import TouchAction
swipElement = driver.find_element_by_id('XXX')
#当前元素的坐标
point = swipElement.location
 # 计算移动后的位置坐标
 pointX = point['x'] + driver.get_window_size()['width']*0.7
 pointY = point['y']
TouchAction(driver).long_press(swipElement).move_to(x=pointX,y=pointY).release().perform()

你可能感兴趣的:(Appium)