最近接触到app操作:滑动屏幕和设置九宫格手势密码,发现挺好玩的,写篇博客记录一下遇到的问题,附代码
滑屏swip
通常打开一个app有引导页,一般是向左滑动几次后才能到主页,appium借助swip方法实现
使用方法 driver.swipe(x1,y1,x2,y2)
其中 x、y是指坐标,由一个坐标位置(x1,y1)滑到另一个坐标位置(x2,y2)
由于手机屏幕尺寸有大有小,所以我们确定x/y时一般要获取手机屏幕大小,使用相对位置滑动
例:
driver.swipe(screen['width'] * 0.9, screen['height'] * 0.6, screen['width'] * 0.2, screen['height'] * 0.4)
九宫格
相信大家有见过九宫格,手工设置九宫格的方法是按住第一个点不放,依次移动到相应的点后松手
借助TouchAction类实现,看类名应该是很好理解的,就是触屏操作
首先进行实例化:
tc = TouchAction(driver)
实现步骤:
tc.press(x=x1, y=y1).wait(200).move_to(x=x2, y=y2).wait(200).move_to(x=x3, y=y3]).wait(200). \ move_to(x=x4, y=y4).wait(200).move_to(x=x5, y=y5).wait(200).release().perform()
完整实现代码
from appium import webdriver import time from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from appium.webdriver.common.mobileby import MobileBy from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.common.multi_action import MultiAction desire_caps = {} desire_caps['platformName'] = 'Android' desire_caps['platformVersion'] = '7.1.2' desire_caps['deviceName'] = 'Android Emulator' desire_caps['appPackage'] = 'xxx' desire_caps['appActivity'] = 'xxx' driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desire_caps) time.sleep(5) # 获取屏幕尺寸 screen = driver.get_window_size() print(screen) # 元素左滑3次 for index in range(3): driver.swipe(screen['width'] * 0.9, screen['height'] * 0.6, screen['width'] * 0.2, screen['height'] * 0.4) time.sleep(5) # 点击立即体验 driver.find_element_by_id('com.xxzb.fenwoo:id/btn_start').click() # 点击登陆 loginbutton = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("注册/登录")') WebDriverWait(driver, 30, 0.2).until(EC.visibility_of_element_located(loginbutton)) driver.find_element(*loginbutton).click() time.sleep(2) # 输入手机号,点击下一步 driver.find_element_by_id('com.xxzb.xxx:id/et_phone').send_keys('xxx') driver.find_element_by_id('com.xxzb.xxx:id/btn_next_step').click() time.sleep(1) # 输入密码,点击确定 driver.find_element_by_id('com.xxzb.xxx:id/et_pwd').send_keys('xxx') driver.find_element_by_id('com.xxzb.xxx:id/btn_next_step').click() # 点击马上设置 confirmbtn = (MobileBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("马上设置")') WebDriverWait(driver, 30, 0.2).until(EC.visibility_of_element_located(confirmbtn)) driver.find_element(*confirmbtn).click() time.sleep(2) # 点击创建手势密码 driver.find_element_by_id('com.xxzb.xxx:id/btn_gesturepwd_guide').click() time.sleep(1) #点击确定 driver.find_element_by_id('com.xxzb.xxx:id/right_btn').click() time.sleep(2) # 设置九宫格 tc = TouchAction(driver) # 查找元素 ele = driver.find_element_by_id('com.xxzb.xxx:id/gesturepwd_create_lockview') # 获取元素大小 size = ele.size # 获取元素起点坐标 location = ele.location print(size, location) point1 = (location['x'] + size['width'] * 1 / 6, location['y'] + size["height"] * 1 / 6) point2 = (point1[0] + size['width'] * 2 / 6, point1[1] + size["height"] * 2 / 6) point3 = (point2[0] + size['width'] * 2 / 6, point2[1] + size["height"] * 2 / 6) point4 = (point3[0] - size['width'] * 2 / 6, point3[1]) point5 = (point4[0] - size['width'] * 2 / 6, point4[1]) point6 = (point5[0], point5[1] - size["height"] * 2 / 6) # 移动元素 tc.press(x=point1[0], y=point1[1]).wait(200).\ move_to(x=point2[0], y=point2[1]).wait(200). \ move_to(x=point3[0], y=point3[1]).wait(200). \ move_to(x=point4[0], y=point4[1]).wait(200). \ move_to(x=point5[0], y=point5[1]).wait(200).\ move_to(x=point6[0], y=point6[1]).wait(200). \ release().perform() time.sleep(2) driver.find_element_by_id('com.xxzb.fenwoo:id/right_btn').click() time.sleep(2) #移动元素 tc.press(x=point1[0], y=point1[1]).wait(200).\ move_to(x=point2[0], y=point2[1]).wait(200). \ move_to(x=point3[0], y=point3[1]).wait(200). \ move_to(x=point4[0], y=point4[1]).wait(200). \ move_to(x=point5[0], y=point5[1]).wait(200).\ move_to(x=point6[0], y=point6[1]).wait(200). \ release().perform() time.sleep(2) #截图 driver.save_screenshot('C:\\Users\\admin\Desktop\\screen.png') driver.find_element_by_id('com.xxzb.xxx:id/right_btn').click() time.sleep(2)
注意:这里本人踩了一个坑:运行的时候报错:'NoneType' object has no attribute 'execute'
浪费了很多时间,最后发现是实例化时没有传递driver,改成tc = TouchAction(driver)就好了