在日常使用App过程中,经常会看到App界面有一些弹窗提示(如下图所示)这些提示元素出现后等待3秒左右就会自动消失,那么我们该如何获取这些元素文字内容呢?
Android中的Toast是一种简易的消息提示框。当视图显示给用户,在应用程序中显示为浮动。和Dialog不一样的是,它永远不会获得焦点,无法被点击。
Toast类的思想就是尽可能不引人注意,同时还向用户显示信息,希望他们看到。而且Toast显示的时间有限,一般3秒左右就消失了。因此使用传统的元素定位工具,我们是无法定位到Toast元素的(传说中低调奢华有内涵)。
Add ability to verify TOAST messages (these can't be interacted with, only text retrieval allowed)
Appium 1.6.3开始支持识别Toast内容,主要是基于UiAutomator2,因此需要在Capablity配置如下参数:
desired_caps['automationName']='uiautomator2'
友情提示:
automationName
设置为uiautomator2
后可能会导致整个appium运行比较慢,如果非必要获取Toast不建议这样设置,直接使用默认的配置desired_caps['automationName']='Appium'
即可
安装appium-uiautomator2-driver: 安装命令如下:
cnpm install appium-uiautomator2-driver
安装成功后可以在 C:\Users\XXXX\node_modules看到对应的文件:
安装selenium模块
pip install selenium
安装完成后使用如下命令检测是否安装成功
#查看selenium版本
C:\Users\Shuqing>pip show selenium
Name: selenium
Version: 3.11.0
Summary: Python bindings for Selenium
Home-page: https://github.com/SeleniumHQ/selenium/
Author: UNKNOWN
Author-email: UNKNOWN
License: Apache 2.0
Location: c:\python35\lib\site-packages
Requires:
Required-by: Appium-Python-Client
selenium模块安装视频教程
进入登录界面输入错误的用户名或者密码,获取Toast内容:
get_toast.py
# coding=utf-8
from find_element.capability import driver
from selenium.webdriver.support.ui import WebDriverWait
driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').clear()
driver.find_element_by_id('com.tal.kaoyan:id/login_email_edittext').send_keys('zxss018')
driver.find_element_by_id('com.tal.kaoyan:id/login_password_edittext').send_keys('zxw2018')
driver.find_element_by_id('com.tal.kaoyan:id/login_login_btn').click()
error_message="用户名或密码错误,你还可以尝试4次"
limit_message="验证失败次数过多,请15分钟后再试"
message='//*[@text=\'{}\']'.format(error_message)
# message='//*[@text=\'{}\']'.format(limit_message)
toast_element=WebDriverWait(driver,5).until(lambda x:x.find_element_by_xpath(message))
print(toast_element.text)
注意:Toast内容为中文时,顶部必须注释# coding=utf-8 否则会因为编解码导致文字识别失败。
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: helpers.installApkRemotely is not a function
修改建议:安装最新版本的Appium
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not sign with default certificate.
修改建议:
Could not proxy command to remote server. Original error: Error: read ECONNRESET
造成该问题的原因一般是appium服务断开了,查看appium运行的详细log我们可以看到
[BaseDriver] Shutting down because we waited 60 seconds for a command
[debug] [UiAutomator2] Deleting UiAutomator2 session
[debug] [UiAutomator2] Deleting UiAutomator2 server session
[debug] [WD Proxy] Matched '/' to command name 'deleteSession'
[debug] [WD Proxy] Proxying [DELETE /] to [DELETE http://localhost:8208/wd/hub/session/72d5a39c-0535-475a-89d6-8fa3875cea1e] with no body
[Appium] Closing session, cause was 'New Command Timeout of 60 seconds expired. Try customizing the timeout using the 'newCommandTimeout' desired capability'
[Appium] Removing session ee196a53-d383-4b7e-a2a2-a3e998a1e879 from our master session list
[HTTP] --> POST /wd/hub/session/7bc053e6-e088-4ff9-a0a9-46246f931dc9/element
[HTTP] {"value":"com.mgtv.data.sdk:id/add_content","using":"id"}
默认是60秒,我们可以设置更大的等待时间间隙,如下所示即可
desired_caps['newCommandTimeout']=2000
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有