【Appium】手机app上toast对象的定位及如何抓取
前言:当在app上点了一个操作之后,一般都会有一个toast弹出来提示你当前操作信息,但是toast停留时间一般都很短,很难直接定位,那该如何去抓取呢?本文提供三种方法。
目录
🍀方法一、抓屏
🍀方法二、通过Xpath定位(提供两种方法)
🌺项目实践
比如我们要抓如下类似的toast,在点击完成之后,会跳出一个"保存成功"的toast,但是它的停留时间都不足1秒,所以没有办法去定位元素。
🍀方法一、抓屏
因为toast停留时间太短,通过元素去抓取的成功率很低,所以抓屏就是最稳妥的方法了,代码很简单,一句话就行了:driver.get_screenshot_as_file('图片名.png')
代码如下:
注意:因为我的toast弹出的较慢,所以加上了一点点的等待时间,正常是不需要的。
def test_toast(self): # 1、抓取截图方法 time.sleep(0.5) # 弹窗跳的有点慢,不加等待时间会抓不到弹窗 self.driver.get_screenshot_as_file('toast.png')
🍀方法二、通过Xpath定位(提供两种方法)
通过观察toast上有文字"保存成功" 所以我们可以通过xpath中包含text文本的方式去抓取到toast。
Xpath编写:(By.XPATH, "//*[@text= '保存成功']")
注意:
1、如果toast内容是中文,要在脚本开头声明 # coding=utf-8 否则会不支持。
提供两种关于WebDriverWait的抓取方法,亲测都能有效输出toast内容。
第一种:
# coding=utf-8from appium.webdriver.webdriver import WebDriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport timedef test_toast(self): # 2、显示等待方法1 # time.sleep(0.5) toast_el = (By.XPATH, "//*[@text= '保存成功']") toast = WebDriverWait(self.driver, 5, 0.01).until(EC.presence_of_element_located(toast_el)) print(toast.text)
第二种:
# coding=utf-8from appium.webdriver.webdriver import WebDriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.wait import WebDriverWaitdef test_toast(self): # 3、显示等待方法2 toast = WebDriverWait(self.driver, 5, 0.1).until(lambda x: x.find_element(By.XPATH, "//*[@text= '保存成功']")) print(toast.text)
🌺项目实践
举的例子是在模拟器上使用有道云app新建笔记之后点击完成跳出的toast,完整代码如下:
# coding=utf-8from appium.webdriver.webdriver import WebDriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport timeclass yd_toast: def __init__(self): self.caps = { 'automationName': 'UiAutomator2', 'platformName': 'Android', 'platformVersion': '6.0', 'deviceName': '192.168.23.101:5555', 'appPackage': 'com.youdao.note', 'appActivity': '.activity2.MainActivity'} self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps) self.driver.implicitly_wait(10) # 进行新增笔记的测试 def test_addnote(self): el = WebDriverWait(self.driver, 10).until( lambda x: x.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_button')) if el: # 点击同意 self.driver.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_button').click() # 点击+号 # self.driver.find_element(By.ID, 'com.youdao.note:id/add_note').click() self.driver.find_element('id', 'com.youdao.note:id/add_note').click() # 点击新建笔记 self.driver.find_element(By.ID, 'com.youdao.note:id/add_note_floater_add_note').click() # 点击取消 self.driver.find_element(By.ID, 'com.youdao.note:id/btn_cancel').click() # 输入内容 self.driver.find_element(By.XPATH, '//*[@resource-id="com.youdao.note:id/note_content"]/android.widget.EditText').send_keys( 'testcontext1234') # 输入标题 self.driver.find_element(By.ID, 'com.youdao.note:id/note_title').send_keys('testtitle') # 点击完成 self.driver.find_element(By.ID, 'com.youdao.note:id/actionbar_complete_text').click() def test_toast(self): # 1、抓取截图方法 # time.sleep(0.5) # 弹窗跳的有点慢,不加等待时间会抓不到弹窗 # self.driver.get_screenshot_as_file('toast.png') # 2、显示等待方法1 # time.sleep(0.5) # toast_el = (By.XPATH, "//*[@text= '保存成功']") # toast = WebDriverWait(self.driver, 5, 0.01).until(EC.presence_of_element_located(toast_el)) # print(toast.text) # 3、显示等待方法2 toast = WebDriverWait(self.driver, 5, 0.1).until(lambda x: x.find_element(By.XPATH, "//*[@text= '保存成功']")) print(toast.text)if __name__ == '__main__': toast_obj = yd_toast() toast_obj.test_addnote() toast_obj.test_toast()
创作打卡挑战赛
赢取流量/现金/CSDN周边激励大奖