1. 程式人生 > >appium基礎:滑屏和畫九宮格踩坑'NoneType' object has no attribute 'execute'

appium基礎:滑屏和畫九宮格踩坑'NoneType' object has no attribute 'execute'

最近接觸到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)就好了