前言:appium必须是1.6以上的版本

环境(安装和安装都可以运行成功,我也不确定要不要这个)

1、抓到toast需要安装uiautomator2,安装npm:npm install -g cnpm --registry=https://registry.npm.taobao.org   

2、 然后通过这个命令: cnpm install appium-uiautomator2-driver   安装uiautomator2的配置文件

一、先看看toast

二、代码实现:

#coding= utf-8
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
desired_caps = {
                'platformName':"Android",
                'deviceName':"R8V5T15930002010",
                'platformVersion':"5.0.1",
                'appPackage':"com.yuedan",
                'appActivity':"com.yuedan.ui.Activity_Splash",
                'unicodeKeyboard': True,#使用unicode编码方式发布字符串
                'resetKeyboard': True , #屏蔽软键盘,
                'automationName': 'uiautomator2',
                'noReset':True
                }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 等主页面activity出现
time.sleep(20)
print(driver.current_activity)
driver.wait_activity(".ui.MainActivity", 20)
search_view = WebDriverWait(driver,10,0.1).until(EC.presence_of_element_located((By.ID, "com.yuedan:id/search_view")))
search_view.click()
search= WebDriverWait(driver,10,0.1).until(EC.presence_of_element_located((By.ID, "com.yuedan:id/iv_search")))
search.click()
toast_message = "请输入搜索关键字"
message ='//*[@text=\'{}\']'.format(toast_message)
# 获取toast提示框内容
toast_element = WebDriverWait(driver,5).until(lambda x:x.find_element_by_xpath(message))
print(toast_element.text)
driver.quit()

运行结果:

三、代码实现

#coding= utf-8
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
desired_caps = {
                'platformName':"Android",
                'deviceName':"R8V5T15930002010",
                'platformVersion':"5.0.1",
                'appPackage':"com.yuedan",
                'appActivity':"com.yuedan.ui.Activity_Splash",
                'unicodeKeyboard': True,#使用unicode编码方式发布字符串
                'resetKeyboard': True , #屏蔽软键盘,
                'automationName': 'uiautomator2'
                }
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
time.sleep(2)
driver.find_element_by_id("com.yuedan:id/login").click()
time.sleep(2)
driver.find_element_by_id("com.yuedan:id/btnQuickLogin").click()
driver.find_element_by_id("com.yuedan:id/et_name").send_keys("39999")
driver.find_element_by_id("com.yuedan:id/tv_re_sent").click()
# 定位toast元素
toast_message = "手机号码格式错误"
message ='//*[@text=\'{}\']'.format(toast_message)
# 获取toast提示框内容
toast_element = WebDriverWait(driver,5,0.1).until(lambda x:x.find_element_by_xpath(message))
print(toast_element.text)
driver.quit()

运行结果: