前言:appium必須是1.6以上的版本web
環境(安裝和安裝均可以運行成功,我也不肯定要不要這個):npm
一、抓到toast須要安裝uiautomator2,安裝npm:npm install -g cnpm --registry=https://registry.npm.taobao.org
bash
二、 而後經過這個命令: cnpm install appium-uiautomator2-driver 安裝uiautomator2的配置文件app
1、先看看toastui
2、代碼實現:編碼
#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()
運行結果:spa
3、代碼實現3d
#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()
運行結果:code