
以微信登錄界面的toast彈窗為例:
我的appium版本是1.17.1,服務器運行時日志有這樣的打印信息

所以我的desired_caps如下(如果是之前的版本,需要安裝uiautomator2,並且在初始化時指定automationName為uiautomator2):
desired_caps_a = {'platformName': "Android",
'platFormVersion': "10",
'deviceName': "dd",
'udid': "648d4f29",
'automationName': "UiAutomator2",
'noReset': False,
'unicodeKeyboard': True,
'resetKeyboard': True,
'appPackage': "com.tencent.mm",
'appActivity': "com.tencent.mm.ui.LauncherUI",
'autoGrantPermissions': True,
'chromedriverExecutable': r"C:\Users\v_yddchen\Desktop\chromedriver_win32 77.0\chromedriver.exe",
'chromeOptions': {'androidProcess': "com.tencent.mm:toolsmp"} # webview的進程名字,不然會報錯
}
填寫登錄信息,獲取toast(有時來不及獲取登錄界面的toast信息,會拿到登錄成功后,加載數據的toast):
device_a = webdriver.Remote("http://localhost:4723/wd/hub", desired_caps_a) my_logging = output_log(logging.DEBUG) # toast彈窗處理 device_a.find_element_by_android_uiautomator('new UiSelector().text("登錄")').click() time.sleep(1) device_a.find_element_by_android_uiautomator('new UiSelector().textContains("用微信號")').click() time.sleep(1) username_input = device_a.find_element_by_android_uiautomator('new UiSelector().textContains("請填寫微信號")') username_input.clear() username_input.send_keys('xxx') pwd_input = device_a.find_element_by_android_uiautomator('new UiSelector().textContains("請填寫密碼")') pwd_input.clear() pwd_input.send_keys('xxx') device_a.find_element_by_android_uiautomator('new UiSelector().text("登錄")').click() toast_el = WebDriverWait(device_a, 5, 0.00001).\ until(lambda x: x.find_element_by_xpath("//*[contains(@text,'正在')]")) # toast_el = WebDriverWait(device_a, 5, 0.000001).\ # until(lambda x: x.find_element_by_xpath('//*[@class="android.widget.Toast"]')) print(toast_el.text) device_a.quit()
當密碼填寫信息錯誤時,不管改定時器的間隔為多小,都找不到toast,比較奇怪。如果點擊【登錄】--登錄完成,跳轉,這個過程很短,也有可能獲取不到toast。
一個未完成的更新......
