Appium獲取toast消息(二)

剛接觸appium進行移動端設備的UI自動化,在遇到toast消息的時候很是苦惱了一陣,最後通過強大的搜索引擎找到了個相對解決方法,廢話不多說,直接貼代碼↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def is_toast_exist(driver,text=None,timeout=30,poll_frequency=0.5):

   '''is toast exist, return True or False
   :Agrs:
    - driver - 傳driver
    - text   - 頁面上看到的文本內容
    - timeout - 最大超時時間,默認30s
    - poll_frequency  - 間隔查詢時間,默認0.5s查詢一次
   :Usage:
    is_toast_exist(driver, "看到的內容")
   '''

   try:
       toast_loc = ("xpath", ".//*[contains(@text,'%s')]"%text)
       WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc))
       return True
   except:
       return False

如果還有更好的方法,歡迎在評論區留言,共同學習

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章