剛接觸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
如果還有更好的方法,歡迎在評論區留言,共同學習