1. id定位:android
self.driver.find_element_by_id('com.tencent.mobileqq:id/btn_login').click()
2. class定位:web
self.driver.find_element_by_class_name('android.widget.Button').click()
(注:通常一個頁面上的class屬性不惟一,元素不惟一的話定位會報錯了)數組
3. 相對定位:服務器
相對定位是先找到該元素的有對應屬性的父元素節點,而後基於父元素進行元素定位。app
代碼舉例:函數
此處只是舉例什麼是相對定位,通常有id直接能夠定位固然不這麼幹,在沒有id的狀況下能夠這麼定位。。。。。。。。。。。工具
self.driver.find_element_by_id('com.tencent.mobileqq:id/name') self.driver.find_element_by_class_name('android.widget.Button').click()
uiautomatorviewer截屏:測試
代碼舉例:ui
name = self.driver.find_element_by_xpath('//android.widget.EditText[@text="QQ號/手機號/郵箱"]').send_keys('********')
name = self.driver.find_element_by_xpath('//*[@class="android.widget.EditText" and @index="3"]').send_keys('********')
uiautomatorviewer截屏:編碼
5. List定位:
List定位首先是使用find_elements_by_id(class_name/path)獲取一組相同的class屬性的的元素,而後使用數組下標來區分標記不一樣元素進行相關操做
代碼舉例:
通常從相冊設置頭像或是選擇照片時,會用到list定位,由於每一張照片的id是相同,那就要經過下標來定位所選的照片了..............
images = self.driver.find_elements_by_id('id') images[5].click
6. Appium元素等待:
強制等待:設置固定的等待時間,使用sleep()方法便可實現
from time import sleep #強制等待5秒 sleep(5)
隱式等待:針對所有元素設置等待時間
driver.implicitly_wait(20)
顯示等待:針對某個元素來設置的等待時間,方法WebDriverWait()通常和until()或until_not()方法配合使用,另外,lambda提供了一個運行時動態建立函數的方法
from selenium.webdriver.support.ui import WebDriverWait WebDriverWait(self.driver,3).until(lambda x:x.find_element_by_id('com.tencent.mobileqq:id/btn_login'))
7. Toast元素識別:
下圖爲通常的toast提示,uiautomatorviewer工具是沒法獲取到這種toast的任何信息
代碼實現:
注意若是內容爲中文,必須註釋#coding=utf-8,不然會由於編碼致使文字識別失敗
#適用toast彈窗
def get_toast(self):
error_message= "帳號或密碼錯誤,請從新輸入"
limit_message=""
message1 ='//*[@text=\'{}\']'.format(error_message)
#message2 = '//*[@text=\'{}\']'.format(limit_message)
toast_element = WebDriverWait(self.driver,5).until(lambda x:x.find_element_by_xpath(message1))
print(toast_element.text)
8. 屏幕截圖:
方法一:save_screenshot()該方法直接保存當前屏幕截圖到當前腳本所在的文件位置
self.driver.save_screenshot('login.png')
方法二:get_screenshot_as_file(self,filename)將截圖保存在指定文件路徑
self.driver.get_screenshot_as_file('.\screenshots\login.png')
9. 連續滑動操做_TouchAction:
Touch Action包含一系列操做,好比按壓,長按,點擊,移動,暫停,組成一套動做。
按壓:press()
TouchAction(driver).press(x=0,y=308)
長按:longPress() ,比press多個按的時間參數duration,以毫秒爲單位
long_press(x=0,y=308,duration=1000)
點擊:tap()對一個元素或是控件執行點擊操做
tap(self, element=None, x=None, y=None, count=1)
移動:move_to()將指針從上一個點一道指定的元素或點
move_to(self, element=None, x=None, y=None)
暫停:Wait(),暫停腳本的執行,單位爲毫秒
wait(self, ms=0)
釋放:release()結束行動,取消屏幕上的指針
release(self)
執行:perform()執行的操做發送到服務器的命令操做
perform(self)
代碼舉例:已設置手勢密碼鎖爲例,先進入密碼鎖的設置頁面:
#導入模塊 from appium.webdriver.common.touch_action import TouchAction for i in range(2): TouchAction(driver).press(x=243,y=381).wait(2000)\ .move_to(x=455,y=390).wait(1000) \ .move_to(x=455, y=390).wait(1000) \ .move_to(x=455, y=390).wait(1000) \ .release().perform()
10. 多點觸控操做_MultiAction:
多點觸控的類,能夠模擬用戶多點操做,主要包含add()和perform()兩個方法
代碼舉例:以放大縮小百度地圖爲例
from appium import webdriver from appium.webdriver.common.touch_action import TouchAction from appium.webdriver.common.multi_action import MultiAction from time import sleep desired_caps = {} desired_caps['platformName'] = 'Android' desired_caps['platformVersion'] = '5.1.1' desired_caps['deviceName'] = '127.0.0.1:62001' desired_caps['app'] = r'C:\Users\heber\Downloads\baidumap.apk' # 被測試的App在電腦上的位置 desired_caps['appPackage'] = 'com.baidu.BaiduMap' desired_caps['appActivity'] = 'com.baidu.baidumaps.WelcomeScreen' desired_caps['noReset'] = True driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) # 啓動app x = driver.get_window_size()['width'] y = driver.get_window_size()['height'] def pinch(): action1 = TouchAction(driver) action2 = TouchAction(driver) pinch_action = MultiAction(driver) action1.press(x=x*0.2, y=y*0.2).wait(1000).move_to(x=x*0.4, y=y*0.4).wait(1000).release() action2.press(x=x*0.8, y=y*0.8).wait(1000).move_to(x=x*0.6, y=y*0.6).wait(1000).release() pinch_action.add(action1,action2) print("start pinch....") pinch_action.perform() def zoom(): action1 = TouchAction(driver) action2 = TouchAction(driver) zoom_action = MultiAction(driver) action1.press(x=x*0.4, y=y*0.4).wait(1000).move_to(x=x*0.2, y=y*0.2).wait(1000).release() action2.press(x=x*0.6, y=y*0.6).wait(1000).move_to(x=x*0.8, y=y*0.8).wait(1000).release() zoom_action.add(action1,action2) print("start zoom....") zoom_action.perform() if __name__ == '__main__': for i in range(3): pinch() for i in range(3): zoom()