toast提示信息獲取


toast獲取的前提條件
1、desired_caps["automationName"]="UiAutomator2"
2、要求安裝jdk1.8 64位及以上。配置其環境變量JAVA_HOME 和path
3、Android 系統5.0以上;(夜神模擬器默認的安卓版本是4.4.2,可以在夜神多開器中創建並啟動一個5.1.1的版本)
4、appium server 版本1.6.3以上
5、xpath 表達查找toast
6、只能等待toast存在,而不能等待可見

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.common.touch_action import TouchAction

#啟動參數
desired_caps={}
desired_caps['automationName']='UiAutomator2'
desired_caps["platformName"] = "Android"
desired_caps["platformVersion"] = "5.1"
desired_caps["deviceName"] = "Android Emulator"
desired_caps["noReset"] = True

#保證終端設備上,已安裝了對應的app。
desired_caps["appPackage"] = "com.lemon.lemonban"
desired_caps["appActivity"] = "com.lemon.lemonban.activity.WelcomeActivity"

#連接appium server,然后告訴它server,我要干什么。
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)

#首頁-點擊我的檸檬
WebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.ID,'com.lemon.lemonban:id/navigation_my')))
driver.find_element_by_id('com.lemon.lemonban:id/navigation_my').click()

#點擊我的頭像-----等待元素可見
WebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.ID,'com.lemon.lemonban:id/fragment_my_lemon_avatar_layout')))
driver.find_element_by_id('com.lemon.lemonban:id/fragment_my_lemon_avatar_layout').click()

#點擊登錄按鈕
WebDriverWait(driver,20).until(EC.visibility_of_element_located((MobileBy.ID,'com.lemon.lemonban:id/btn_login')))
driver.find_element_by_id('com.lemon.lemonban:id/btn_login').click()

#獲取toast信息
#toast:使用xpath表達式
#不能用等待元素可見。只能用等待元素存在
toast_loc = '//*[contains(@text,"手機號碼或密碼")]'
try:
WebDriverWait(driver,5,0.01).until(EC.presence_of_all_elements_located((MobileBy.XPATH,toast_loc)))
print(driver.find_element_by_xpath(toast_loc).text)
except:
print('沒有獲取到toast信息')

#注意事項:注意等待的時間,toast等待時間要快一點
 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM