Appium appium 元素定位错误

· 2016年02月19日 · 最后由 81—1 回复于 2016年02月22日 · 1277 次阅读

通过 xpath 定位总是会定位到错误的元素,导致报错。
我要定位的是"报告异常" ,总是定位到 "个人"
如下是详细 appium 日志:

2016-02-19 16:31:17:552 - info: <-- POST /wd/hub/session/31041efc-ba77-4b5c-a689-2192c937088a/element/3/click 200 1356.006 ms - 74 {"status":0,"value":"","sessionId":"31041efc-ba77-4b5c-a689-2192c937088a"}

2016-02-19 16:31:17:557 - info: --> POST /wd/hub/session/31041efc-ba77-4b5c-a689-2192c937088a/element {"using":"xpath","value":"//UIAApplication[1]/UIAWindow[1]/UIAButton[3]"}
2016-02-19 16:31:17:557 - info: [debug] Waiting up to 0ms for condition
2016-02-19 16:31:17:557 - info: [debug] Pushing command to appium work queue: "au.mainApp().getTreeForXML()"
2016-02-19 16:31:17:558 - info: [debug] Sending command to instruments: au.mainApp().getTreeForXML()
2016-02-19 16:31:17:566 - info: [debug] [INST] 2016-02-19 08:31:17 +0000 Debug: evaluation finished
2016-02-19 16:31:17:568 - info: [debug] [INST] 2016-02-19 08:31:17 +0000 Debug: responding with:
2016-02-19 16:31:17:571 - info: [debug] [INST] 2016-02-19 08:31:17 +0000 Debug: Running system command #16: /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{"status":0,"value":""}...

2016-02-19 16:31:18:585 - info: [debug] [INST] 2016-02-19 08:31:18 +0000 Debug: Got new command 16 from instruments: au.mainApp().getTreeForXML()

2016-02-19 16:31:18:588 - info: [debug] [INST] 2016-02-19 08:31:18 +0000 Debug: evaluating au.mainApp().getTreeForXML()

2016-02-19 16:31:20:769 - info: [debug] [INST] 2016-02-19 08:31:20 +0000 Debug: evaluation finished

{"UIAButton":{"@":{"name":"报告异常","label":"报告异常","value":null,"dom":null,"enabled":true,"valid":true,"visible":true,"hint":null,"path":"/0/0/11","x":27,"y":518,"width":120,"height":39},">":[]}}

{"UIAElement":{"@":{"name":"个人","label":"个人","value":null,"dom":null,"enabled":true,"valid":true,"visible":false,"hint":null,"path":"/0/0/18","x":160,"y":568,"width":80,"height":49},">":[]}}

2016-02-19 16:31:20:780 - info: [debug] [INST] 2016-02-19 08:31:20 +0000 Debug: Running system command #17: /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{"status":0,"value":{"UIAApplication":{"@":{"name":"卡卡测试版","label":"卡卡测试版","value":null,"dom":null,"e...
2016-02-19 16:31:20:867 - info: [debug] Socket data received (7826 bytes)
2016-02-19 16:31:20:867 - info: [debug] Socket data received (7856 bytes)
2016-02-19 16:31:20:868 - info: [debug] Socket data received (5839 bytes)
2016-02-19 16:31:20:868 - info: [debug] Socket data being routed.
2016-02-19 16:31:20:868 - info: [debug] Got result from instruments: {"status":0,"value":{"UIAApplication":{"@":{"name":"卡卡测试版","label":"卡卡测试版","value":null,"dom":null,"enabled":true,"valid":true,"visible":true,"hint":null,"path":"/0","x":0,"y":0,"width":320,"height":568},">":[{"UIAWindow":{"@":{"name":null,"label":null,"value":null,"dom":null,"enabled":true,"valid":
2016-02-19 16:31:20:908 - info: [debug] Pushing command to appium work queue: "au.getElementByIndexPath('/0/0/11')"
2016-02-19 16:31:20:908 - info: [debug] Sending command to instruments: au.getElementByIndexPath('/0/0/11')

2016-02-19 16:31:21:871 - info: [debug] [INST] 2016-02-19 08:31:21 +0000 Debug: Got new command 17 from instruments: au.getElementByIndexPath('/0/0/11')

2016-02-19 16:31:21:874 - info: [debug] [INST] 2016-02-19 08:31:21 +0000 Debug: evaluating au.getElementByIndexPath('/0/0/11')

2016-02-19 16:31:21:879 - info: [debug] [INST] 2016-02-19 08:31:21 +0000 Debug: evaluation finished
2016-02-19 16:31:21:883 - info: [debug] [INST] 2016-02-19 08:31:21 +0000 Debug: Lookup returned [object UIAElement] with the name "个人" (id: 4).
2016-02-19 16:31:21:886 - info: [debug] [INST] 2016-02-19 08:31:21 +0000 Debug: responding with:
2016-02-19 16:31:21:891 - info: [debug] [INST] 2016-02-19 08:31:21 +0000 Debug: Running system command #18: /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{"status":0,"value":{"ELEMENT":"4"}}...

2016-02-19 16:31:21:967 - info: [debug] Socket data received (38 bytes)
2016-02-19 16:31:21:968 - info: [debug] Socket data being routed.
2016-02-19 16:31:21:968 - info: [debug] Got result from instruments: {"status":0,"value":{"ELEMENT":"4"}}
2016-02-19 16:31:21:968 - info: [debug] Responding to client with success: {"status":0,"value":{"ELEMENT":"4"},"sessionId":"31041efc-ba77-4b5c-a689-2192c937088a"}
2016-02-19 16:31:21:970 - info: <-- POST /wd/hub/session/31041efc-ba77-4b5c-a689-2192c937088a/element 200 4412.745 ms - 87 {"status":0,"value":{"ELEMENT":"4"},"sessionId":"31041efc-ba77-4b5c-a689-2192c937088a"}
2016-02-19 16:31:21:973 - info: --> POST /wd/hub/session/31041efc-ba77-4b5c-a689-2192c937088a/element/4/click {"id":"4"}
2016-02-19 16:31:21:974 - info: [debug] Pushing command to appium work queue: "au.tapById('4')"
2016-02-19 16:31:21:974 - info: [debug] Sending command to instruments: au.tapById('4')

2016-02-19 16:31:22:810 - info: [IOS_SYSLOG_ROW ] Feb 19 16:31:22 PENGBillZhang MobileGestaltHelper[87] <Error>: libMobileGestalt MobileGestalt.c:276: server_access_check denied access to question UniqueDeviceID for pid 1452

2016-02-19 16:31:22:815 - info: [IOS_SYSLOG_ROW ] Feb 19 16:31:22 PENGBillZhang ScriptAgent[1452] <Error>: libMobileGestalt MobileGestaltSupport.m:151: pid 1452 (ScriptAgent) does not have sandbox access for re6Zb+zwFKJNlkQTUeT+/w and IS NOT appropriately entitled

2016-02-19 16:31:22:819 - info: [IOS_SYSLOG_ROW ] Feb 19 16:31:22 PENGBillZhang ScriptAgent[1452] <Error>: libMobileGestalt MobileGestalt.c:537: no access to UniqueDeviceID (see <rdar://problem/11744455>)
2016-02-19 16:31:22:834 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Debug: Got new command 18 from instruments: au.tapById('4')
2016-02-19 16:31:22:838 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Debug: evaluating au.tapById('4')
2016-02-19 16:31:22:841 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Debug: target.frontMostApp().elements()[0].elements()[11].tap()
2016-02-19 16:31:22:843 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Debug: target.frontMostApp().elements()[0].elements()[11].scrollToVisible()
2016-02-19 16:31:22:846 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Debug: target.frontMostApp().elements()[0].elements()[11] - scrollToVisible cannot be used because this element has no scrollable ancestor.
2016-02-19 16:31:22:848 - info: [IOS_SYSLOG_ROW ] Feb 19 16:31:22 PENGBillZhang MobileGestaltHelper[87] <Error>: libMobileGestalt MobileGestalt.c:276: server_access_check denied access to question UniqueDeviceID for pid 1452
2016-02-19 16:31:22:849 - info: [IOS_SYSLOG_ROW ] Feb 19 16:31:22 PENGBillZhang ScriptAgent[1452] <Error>: libMobileGestalt MobileGestaltSupport.m:151: pid 1452 (ScriptAgent) does not have sandbox access for re6Zb+zwFKJNlkQTUeT+/w and IS NOT appropriately entitled
2016-02-19 16:31:22:851 - info: [IOS_SYSLOG_ROW ] Feb 19 16:31:22 PENGBillZhang ScriptAgent[1452] <Error>: libMobileGestalt MobileGestalt.c:537: no access to UniqueDeviceID (see <rdar://problem/11744455>)
2016-02-19 16:31:22:853 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Debug: target.frontMostApp().elements()[0].elements()[11] - scrollToVisible cannot be used because this element has no scrollable ancestor.
2016-02-19 16:31:22:856 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Debug: target.frontMostApp().elements()[0].elements()[11] could not be tapped because the element is not visible

2016-02-19 16:31:23:132 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Error: VerboseError: elementId 4 could not be tapped

2016-02-19 16:31:23:156 - info: [debug] Socket data received (57 bytes)

2016-02-19 16:31:23:157 - info: [debug] Socket data being routed.
2016-02-19 16:31:23:158 - info: [debug] Got result from instruments: {"status":13,"value":"elementId 4 could not be tapped"}
2016-02-19 16:31:23:159 - info: [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command.","origValue":"elementId 4 could not be tapped"},"sessionId":"31041efc-ba77-4b5c-a689-2192c937088a"}
2016-02-19 16:31:23:161 - info: <-- POST /wd/hub/session/31041efc-ba77-4b5c-a689-2192c937088a/element/4/click 500 1187.785 ms - 200 

2016-02-19 16:31:23:273 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Error: Error during eval: [native code]
file:///Applications/Appium.app/Contents/Resources/node_modules/appium/6CAB92C8-C959-404B-8E66-9B853355EABE/bootstrap-5923cadea0343e3f.js:711:30
tapById@file:///Applications/Appium.app/Contents/Resources/node_modules/appium/6CAB92C8-C959-404B-8E66-9B853355EABE/bootstrap-5923cadea0343e3f.js:1100:42
eval code
eval@[native code]
startProcessing@file:///Applications/Appium.app/Contents/Resources/node_modules/appium/6CAB92C8-C959-404B-8E66-9B853355EABE/bootstrap-5923cadea0343e3f.js:2710:30
bootstrap@file:///Applications/Appium.app/Contents/Resources/node_modules/appium/6CAB92C8-C959-404B-8E66-9B853355EABE/bootstrap-5923cadea0343e3f.js:2778:31
global code@file:///Applications/Appium.app/Contents/Resources/node_modules/appium/6CAB92C8-C959-404B-8E66-9B853355EABE/bootstrap-5923cadea0343e3f.js:2787:10

2016-02-19 16:31:23:277 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Debug: responding with:
2016-02-19 16:31:23:281 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Debug: Running system command #19: /Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/submodules/appium-uiauto/bin/command-proxy-client.js /tmp/instruments_sock 2,{"status":13,"value":"elementId 4 could not be tapped"}...
共收到 14 条回复 时间 点赞

你倒不如给 uiautomatorviewer 的截图,还有你写的 xpath

//UIAApplication[1]/UIAWindow[1]/UIAButton[3]
这表达式匹配出 2 个元素. 你把//换/试试吧

#3 · 2016年02月20日 Author

#2 楼 @seveniruby 换成/ 之后直接提示找不到元素 , 这个表达式的意思应该就是找当前应用当前窗口中的第三个按键吧? 可是最后找到的是这个"个人"的类型并不是 button ,怎么会这样。

#4 · 2016年02月20日 Author

#1 楼 @huanzhijin xpath 就是//UIAApplication[1]/UIAWindow[1]/UIAButton[3]

#5 楼 @clovey 别 @ 这么多人. 你直接换个 xpath 表达式不就行了. //*[@name='报告异常'] 不就行了.

有 Name,直接用 findElementByName(“报告异常”),多省事,直接忽略 xpath 类似造成的影响

#8 · 2016年02月21日 Author

#6 楼 @seveniruby 换了 xpath 表达式还是不行,找到的还是其他的元素。。。

#9 · 2016年02月21日 Author

#7 楼 @fingtinload 是这样的 , 报告异常这个按钮在两个页面都有 ,我用 name 总是找到另外一个页面的 button ,但是那个 button 是 not visible 。所以我才用 xpath ,但是 xpath 总是在最后一步 appium 返回查找的值时返回错误的元素信息。。。

用 OS X 的 inspector 试下,把录制的 xpath 对比看下,比较安逸。自己找的估计有遗漏。

#11 · 2016年02月22日 Author

#10 楼 @t880216t 这个 xpath 我就是用 inspector 找的

#11 楼 @clovey 你试试一层一层往下手动定位调试看看,基本都是某个 index 有问题。

#13 · 2016年02月22日 Author

#12 楼 @t880216t 感觉就是这里出的问题

2016-02-19 16:31:21:874 - info: [debug] [INST] 2016-02-19 08:31:21 +0000 Debug: evaluating au.getElementByIndexPath('/0/0/11')

2016-02-19 16:31:21:879 - info: [debug] [INST] 2016-02-19 08:31:21 +0000 Debug: evaluation finished
2016-02-19 16:31:21:883 - info: [debug] [INST] 2016-02-19 08:31:21 +0000 Debug: Lookup returned [object UIAElement] with the name "个人" (id: 4).

从日志里看,报告异常的indexpath 就是"path":"/0/0/11"  ,这是没问题 的  但最后evaluating 出来的结果是个人 , 而个人的indexpath是"path":"/0/0/18"  好奇怪

#13 楼 @clovey 问题在这:
2016-02-19 16:31:22:853 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Debug: target.frontMostApp().elements()[0].elements()[11] - scrollToVisible cannot be used because this element has no scrollable ancestor.
2016-02-19 16:31:22:856 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Debug: target.frontMostApp().elements()[0].elements()[11] could not be tapped because the element is not visible

2016-02-19 16:31:23:132 - info: [debug] [INST] 2016-02-19 08:31:22 +0000 Error: VerboseError: elementId 4 could not be tapped

肯定还是定位到的位置不对,发脚本和 Appium Inspector 截图看看。

关闭了讨论 05月04日 14:26
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册