使用方法 find_element_by_android_uiautomator() 可以運(yùn)用UiAutomator元素定位
1、id定位:
id定位是根據(jù)元素的resource-id屬性來進(jìn)行定位,使用 UiSelector().resourceId()方法
# 用戶名輸入框清除:通過id
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.tal.kaoyan:id/login_email_edittext")').clear()
# 用戶名輸入框輸入賬戶:通過id
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.tal.kaoyan:id/login_email_edittext")').send_keys("zxw123")
2、text定位:
text定位就是根據(jù)元素的text屬性值來進(jìn)行定位,使用 new UiSelector().text()方法
# 用戶名輸入框清除:通過text
driver.find_element_by_android_uiautomator('new UiSelector().text("請輸入用戶名")').clear()
# 用戶名輸入框輸入賬戶:通過text
driver.find_element_by_android_uiautomator('new UiSelector().text("請輸入用戶名")').send_keys("zxw123")
3、class name定位:
與Appium class定位方式一樣,也是根據(jù)元素的class屬性來進(jìn)行定位,使用new UiSelector().className()方法
# 用戶名輸入框清除:通過class_name
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.EditText")').clear()
# 用戶名輸入框輸入賬戶:通過class_name
driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.EditText")').send_keys("zxw123")
舉例:
# -*-coding:utf-8 -*-
# @Author : hudechao
# @Time : 2022/1/20 2:07
from AppiumTest import findElement
"""
等錄kyb:
1、檢測是否第一次登錄,
2、是第一次登錄,調(diào)直接登錄方法,
3、不是第一次登錄,點擊"我"-"未登錄"-調(diào)用直接登錄方法
"""
driver = findElement.driver()
def login():
# 用戶名輸入框清除:通過id
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.tal.kaoyan:id/login_email_edittext")').clear()
# 用戶名輸入框輸入賬戶:通過id
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.tal.kaoyan:id/login_email_edittext")').send_keys("zxw123")
# # 用戶名輸入框清除:通過text
# driver.find_element_by_android_uiautomator('new UiSelector().text("請輸入用戶名")').clear()
# # 用戶名輸入框輸入賬戶:通過text
# driver.find_element_by_android_uiautomator('new UiSelector().text("請輸入用戶名")').send_keys("zxw123")
# # 用戶名輸入框清除:通過class_name
# driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.EditText")').clear()
# # 用戶名輸入框輸入賬戶:通過class_name
# driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.EditText")').send_keys("zxw123")
# 密碼輸入框輸入密碼
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")').send_keys("zxw123")
# 點擊登錄按鈕
driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.tal.kaoyan:id/login_login_btn")').click()
def checkCancelButton():
try:
# 如果是首次安裝運(yùn)行,會有升級提示
cancelButton = driver.find_element_by_id("android:id/button2")
except Exception as e:
print("cancelButton is not exist")
else:
cancelButton.click()
print("點擊取消按鈕")
def checkSkipButton():
try:
# 升級提示后的跳過按鈕
skipButton = driver.find_element_by_id("com.tal.kaoyan:id/tv_skip")
except Exception as e:
print("skipButton is not exist")
else:
skipButton.click()
print("點擊跳過按鈕")
try:
# 檢查是否已經(jīng)登錄過,如果登錄過,"我"按鈕元素存在
driver.find_element_by_id("com.tal.kaoyan:id/mainactivity_button_mysefl")
except Exception as e:
print("首次登錄")
checkCancelButton()
checkSkipButton()
login()
else:
driver.find_element_by_id("com.tal.kaoyan:id/mainactivity_button_mysefl").click()
driver.find_element_by_id("com.tal.kaoyan:id/activity_usercenter_username").click()
print("再次登錄")
login()
UIAutomator定位簡介
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 安裝adb 安裝python-uiautomator2pip install --pre -U uiautomat...
- #相同的classname屬性值元素?zé)o法區(qū)分定位,使用list定位解決該問題。list定位首先是使用find_el...
- appium支持5種定位方式: 通過元素ID定位:resource-id 通過CLASS_NAME:class 通...
- 來自APP Android端自動化測試初學(xué)者的筆記,寫的不對的地方大家多多指教哦。 在之前有介紹一些平時常用的元素...