感谢论坛,在这里查到了很多关于 appium webview 的资料,解决了项目的问题。

进入正题,
问题是这样的:使用微信扫描二维码后,进入一个 订单确认页面,如下图,这个页面上的元素怎么都无法定位,不管是通过 id,classname,还是 xpath

比如这样:
self.driver.find_element_by_id('com.tencent.mm:id/dyg').click()

执行的时候一直提示找不到元素,appium 日志如下:

> info: [debug] Waiting up to 15000ms for condition
> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"com.tencent.mm:id/dyg","context":"","multiple":false}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.tencent.mm:id/dyg","context":"","multiple":false}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: find
> info: [debug] [BOOTSTRAP] [debug] Finding com.tencent.mm:id/dyg using ID with the contextId:  multiple: false
> info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.tencent.mm:id/dyg]
> info: [debug] [BOOTSTRAP] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
> info: [debug] [BOOTSTRAP] [debug] Finding com.tencent.mm:id/dyg using ID with the contextId:  multiple: false
> info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.tencent.mm:id/dyg]
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":7,"value":"No element found"}
> info: [debug] Waited for 3071ms so far
> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"com.tencent.mm:id/dyg","context":"","multiple":false}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.tencent.mm:id/dyg","context":"","multiple":false}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: find
> info: [debug] [BOOTSTRAP] [debug] Finding com.tencent.mm:id/dyg using ID with the contextId:  multiple: false
> info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.tencent.mm:id/dyg]
> info: [debug] [BOOTSTRAP] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
> info: [debug] [BOOTSTRAP] [debug] Finding com.tencent.mm:id/dyg using ID with the contextId:  multiple: false
> info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.tencent.mm:id/dyg]
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":7,"value":"No element found"}
> info: [debug] Waited for 13779ms so far
> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"id","selector":"com.tencent.mm:id/dyg","context":"","multiple":false}]
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"id","selector":"com.tencent.mm:id/dyg","context":"","multiple":false}}
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
> info: [debug] [BOOTSTRAP] [debug] Got command action: find
> info: [debug] [BOOTSTRAP] [debug] Finding com.tencent.mm:id/dyg using ID with the contextId:  multiple: false
> info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.tencent.mm:id/dyg]
> info: [debug] [BOOTSTRAP] [debug] Failed to locate element. Clearing Accessibility cache and retrying.
> info: [debug] [BOOTSTRAP] [debug] Finding com.tencent.mm:id/dyg using ID with the contextId:  multiple: false
> info: [debug] [BOOTSTRAP] [debug] Using: UiSelector[INSTANCE=0, RESOURCE_ID=com.tencent.mm:id/dyg]
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":7,"value":"No element found"}
> info: [debug] Condition unmet after 16839ms. Timing out.

环境:appium 1.4.0.0,手机 Android 7.0

请教是什么问题导致这个问题的呢,要如何正确定位这个元素呢?
ps.(tap 坐标的方式可以点击,不过还是想了解一下 这个原因),如果有大神知道,请指点😫 😫


问题补充:
执行到当前页面的时候,使用 driver.page_source 输出的竟然不是当前页面的内容,而是 Android 屏幕首页的元素信息(按 Home 键之后 返回的屏幕首页),好奇怪
其他页面 使用 driver.page_source 输出的内容 是当前页面的内容,元素也可以正常识别
发现这个支付界面 是一个新的 activity,但是输出 current_activity 的时候,appium 已经自动切换到了这个新的 activity,不知道元素无法识别是否跟这个有关系呢.
经确认,没关系,删除


结论:
感谢大家的回复和关注,这几天空余的时候一直在查资料,终于找到了原因
确实是 Android 7.0 在切换 activity 后的一个 bug,bug 链接如下,是说在切换 activity 后导致元素无法定位:
https://github.com/appium/appium/issues/8956
先试着换了一台 Android 6.0 的机器,发现脚本运行没问题,对象也可以正常识别😖 😖
本来想 换成 uiautomator2 继续测试这个问题,发现 论坛上一个帖子介绍说 uiautomator2 需要 appium 1.63 以上的版本,不然会有冲突(感谢 wuranxu 同学)
https://testerhome.com/topics/9684

我这边 appium 的版本是 1.4.0.0,项目一直在用,怕升上去后 这个问题修复了 又出现其他未知的错误,出于稳定方面的考虑,暂时就保持现状,这个问题先用 tap 解决。总算明白了问题的原因😖 😖

有同样问题的同学可以尝试下这个解决方案,用 UIautomator2 来识别。

ps. 切换 activity 后 元素不能正常识别的问题 应该是小范围存在,不是每次切换 必然出现,之前一直不敢确认 是 activity 切换导致的,就是因为 支付页面也是切换了 activity,但是元素就能正常识别,被误导了,走了好多弯路😖 😖


↙↙↙阅读原文可查看相关链接,并与作者交流