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等待时间要快一点
 


猜你喜欢

转载自www.cnblogs.com/sophia-985935365/p/10756113.html