Appium获取toast消息(二)

刚接触appium进行移动端设备的UI自动化,在遇到toast消息的时候很是苦恼了一阵,最后通过强大的搜索引擎找到了个相对解决方法,废话不多说,直接贴代码↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓:

 1 from selenium.webdriver.support.ui import WebDriverWait
 2 from selenium.webdriver.support import expected_conditions as EC
 3  
 4 def is_toast_exist(driver,text=None,timeout=30,poll_frequency=0.5):
 5  
 6    '''is toast exist, return True or False
 7    :Agrs:
 8     - driver - 传driver
 9     - text   - 页面上看到的文本内容
10     - timeout - 最大超时时间,默认30s
11     - poll_frequency  - 间隔查询时间,默认0.5s查询一次
12    :Usage:
13     is_toast_exist(driver, "看到的内容")
14    '''
15  
16    try:
17        toast_loc = ("xpath", ".//*[contains(@text,'%s')]"%text)
18        WebDriverWait(driver, timeout, poll_frequency).until(EC.presence_of_element_located(toast_loc))
19        return True
20    except:
21        return False

如果还有更好的方法,欢迎在评论区留言,共同学习

原文地址:https://www.cnblogs.com/soner/p/9676048.html