Appium [求助!求助!] Appium 想抓取 toast 信息,通过 selendroid 模式 运行脚本,但是提示 Element was not found,请各位指导一二

又又 · 2016年06月14日 · 最后由 wangtian 回复于 2017年06月11日 · 1880 次阅读

问题描述:作为 Appium 初学者,想实现如下功能,

  1. app 登录界面,输入错误的 “用户名” 或 “密码”,点击登录
  2. Toast 提示 “账号或密码错误”
  3. 想捕获这个 Toast,并对其内容进行验证

参考文章:https://testerhome.com/topics/2715,写的简单脚本如下

现在的结果:
无法捕获到 Toast,提示 Element was not found

疑问:不知道到底是原理出了问题,还是因为 message 是 “中文” 的缘故,找不到元素

请各位帮忙,指导一下,在此谢过

===========================================================================================

**python   脚本代码如下**


 # -*- coding: utf-8 -*-  
from appium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '4.4.4'
desired_caps['deviceName'] = 'Android Emulator'
desired_caps['appPackage'] = 'com.mpr.mprepubreader'
desired_caps['appActivity'] = '.activity.SplashActivity'
desired_caps['automationName']='Selendroid'
desired_caps['unicodeKeyboard']= True
desired_caps['resetKeyboard']= True
desired_caps['app'] = 'F:\\02_PhoneTesting\\Appium\\Killer-Android-release-test.apk'


driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)


def find_toast(message, timeout, poll_frequency, driver):
      element = WebDriverWait(driver,timeout,poll_frequency).until(expected_conditions.presence_of_element_located((By.PARTIAL_LINK_TEXT, message)))
      return element

time.sleep(5)
driver.find_element_by_id("login_account").clear()
driver.find_element_by_id("login_account").send_keys("13168732222")
driver.find_element_by_id("login_password_edit").clear()
driver.find_element_by_id("login_password_edit").send_keys("1234567")
driver.find_element_by_id("login_btn_bg_btn").click()

find_toast(u"账号或密码错误", 5, 1, driver)

=============================================================================================

PyCharm 中,运行的结果是:

C:\Python27\python.exe F:/02_PhoneTesting/Appium/Scripts/Test1.py
Traceback (most recent call last):
  File "F:/02_PhoneTesting/Appium/Scripts/Test1.py", line 34, in <module>
    find_toast(u"账号或密码错误", 5, 1, driver)
  File "F:/02_PhoneTesting/Appium/Scripts/Test1.py", line 25, in find_toast
    element = WebDriverWait(driver,timeout,poll_frequency).until(expected_conditions.presence_of_element_located((By.PARTIAL_LINK_TEXT, message)))
  File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 


Process finished with exit code 1

===============================================================================================

**Appium Server的日志是:**

info: [debug] [SELENDROID ERR]  ... 22 more

info: --> POST /wd/hub/session/787653a7-dbdb-3d82-648d-bc3a1f508214/element {"using":"partial link text","sessionId":"787653a7-dbdb-3d82-648d-bc3a1f508214","val
ue":"账号或密码错误"}

info: [debug] Proxying command to localhost:8080

info: [debug] Making http request with opts: {"url":"http://localhost:8080/wd/hub/session/787653a7-dbdb-3d82-648d-bc3a1f508214/element","method":"POST","json":{
"using":"partial link text","sessionId":"787653a7-dbdb-3d82-648d-bc3a1f508214","value":"账号或密码错误"}}

info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.",
"origValue":"Element was not found."},"sessionId":"787653a7-dbdb-3d82-648d-bc3a1f508214"}

共收到 10 条回复 时间 点赞

https://testerhome.com/topics/2715 这个文章里 提到的问题,是不是你的问题?

大家好,我的奇葩问题终于解决。

原因是,selendroid 版本 0.15 之后,就不支持(有 bug)find link text 来 check toast
而我之前用的 appium 版本,1.4.16, 底层是用的 selendroid 0.17..

所以我需要把自己的 appium 版本降级到旧版本,比如 1.4.8(底层引用 selendroid 0.15)才可以工作
https://github.com/selendroid/selendroid/issues/978

text 应该是不支持了,换 xpath,用//*[contains(@text, \"%s\")] 这个格式试试

#2 楼 @shljsh 你好,按照您说的,我试了一下,还是不行。

python 代码:

def find_toast(message, timeout, poll_frequency, driver):
    element = WebDriverWait(driver,timeout,poll_frequency).until(expected_conditions.presence_of_element_located(driver.find_element_by_xpath(message)))
    return element
time.sleep(5)
driver.find_element_by_id("login_account").clear()
driver.find_element_by_id("login_account").send_keys("13168732222")
driver.find_element_by_id("login_password_edit").clear()
driver.find_element_by_id("login_password_edit").send_keys("1234567")
driver.find_element_by_id("login_btn_bg_btn").click()

find_toast("//*[contains(@text, '账号或密码错误')]", 5, 0.5, driver)

运行日志:

C:\Python27\python.exe F:/02_PhoneTesting/Appium/Scripts/Test1.py
Traceback (most recent call last):
  File "F:/02_PhoneTesting/Appium/Scripts/Test1.py", line 37, in <module>
    find_toast("//*[contains(@text, '账号或密码错误')]", 5, 0.5, driver)
  File "F:/02_PhoneTesting/Appium/Scripts/Test1.py", line 26, in find_toast
    element = WebDriverWait(driver,timeout,poll_frequency).until(expected_conditions.presence_of_element_located(driver.find_element_by_xpath(message)))
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 258, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 712, in find_element
    {'using': by, 'value': value})['value']
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\appium\webdriver\errorhandler.py", line 29, in check_response
    raise wde
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

#3 楼 @sunya 那可能就是不支持 toast,我也没找过 toast……

#3 楼 @sunya 你没看到你的帖子我编辑过了么,请用代码块啊

#1 楼 @lihuazhang

你好,我看了那篇帖子,并尝试了

把 appium 版本降低到 1.4.13.1 和 1.4.0,再运行脚本,还是不行(我是 win + Android)

#5 楼 @lihuazhang 好的,谢谢提醒

desired_caps['app'] =''
添加了 APP 的路径之后,appium 每次都会重新安装一次应用啊?

楼主解决这个问题了吗????

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册