Appium UiAutomator 不能识别到的元素,如何定位该元素

酷火 · 2015年06月18日 · 最后由 saii 回复于 2016年01月25日 · 3187 次阅读

使用 UiAutomator 定位元素,在地图导航的页面中,如下图:
插入图片:

上图的 “设终点”,不能定位到,请问有什么方法可以定位。 目前,只想到利用坐标点击。请问大家有什么好的方法?

共收到 16 条回复 时间 点赞

你要先搞清楚地图是用 webview 还是别的东西,然后直接去看 page_source 里面的内容。
uiautomatorviewer 的点击控件位置直接选中控件节点这个功能不是万能的。

酷火 #15 · 2015年06月18日 Author

在上图的界面下运行:

Set<String> contextNames = driver.getContextHandles();
for (String contextName : contextNames) {
  System.out.println(contextName); // 用于返回被测app是NATIVE_APP还是WEBVIEW,如果两者都有就是混合型App
}

得到的打印结果是:
NATIVE_APP

应该不是 webview。

添加代码:

System.out.println(driver.getPageSource());//打印整个页面的元素

以下为 log,但好像没有 “设终点” 的内容。(PS:新人,很多不了解)

<?xml version="1.0" encoding="UTF-8"?><hierarchy rotation="0"><android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1776]" resource-id="" instance="0"><android.widget.LinearLayout index="0" text="" class="android.widget.LinearLayout" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,0][1080,1776]" resource-id="" instance="0"><android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,75][1080,1776]" resource-id="android:id/content" instance="1"><android.widget.RelativeLayout index="0" text="" class="android.widget.RelativeLayout" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,75][1080,1776]" resource-id="com.autonavi.xmgd.navigator:id/layout_map_info_id" instance="0"><android.widget.FrameLayout index="0" text="" class="android.widget.FrameLayout" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,75][1080,219]" resource-id="com.autonavi.xmgd.navigator:id/map_showmap_title" instance="2"><android.widget.RelativeLayout index="0" text="" class="android.widget.RelativeLayout" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,75][1080,219]" resource-id="" instance="1"><android.widget.ImageButton NAF="true" index="0" text="" class="android.widget.ImageButton" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,75][126,219]" resource-id="com.autonavi.xmgd.navigator:id/gdtitle_left" instance="0"/><android.widget.TextView index="1" text="请选择终点" class="android.widget.TextView" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[126,106][954,187]" resource-id="com.autonavi.xmgd.navigator:id/gdtitle_text" instance="0"/></android.widget.RelativeLayout><android.widget.RelativeLayout index="1" text="" class="android.widget.RelativeLayout" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,75][1080,219]" resource-id="" instance="2"><android.widget.ImageButton NAF="true" index="0" text="" class="android.widget.ImageButton" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[0,75][126,219]" resource-id="com.autonavi.xmgd.navigator:id/gdtitle_left" instance="1"/><android.widget.TextView index="1" text="" class="android.widget.TextView" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[126,106][954,187]" resource-id="com.autonavi.xmgd.navigator:id/gdtitle_text" instance="1"/></android.widget.RelativeLayout></android.widget.FrameLayout><android.widget.ImageButton NAF="true" index="1" text="" class="android.widget.ImageButton" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[900,234][1065,399]" resource-id="com.autonavi.xmgd.navigator:id/map_tmc" instance="2"/><android.widget.RelativeLayout index="2" text="" class="android.widget.RelativeLayout" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[900,429][1065,594]" resource-id="com.autonavi.xmgd.navigator:id/map_maplayerdrawer_out" instance="3"><android.widget.ImageButton index="0" text="" class="android.widget.ImageButton" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[900,429][1065,594]" resource-id="com.autonavi.xmgd.navigator:id/map_maplayerdrawer" instance="3"/></android.widget.RelativeLayout><android.widget.LinearLayout index="3" text="" class="android.widget.LinearLayout" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[900,1166][1065,1511]" resource-id="com.autonavi.xmgd.navigator:id/map_zoom" instance="1"><android.widget.LinearLayout index="0" text="" class="android.widget.LinearLayout" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[900,1166][1065,1511]" resource-id="" instance="2"><android.widget.ImageView NAF="true" index="0" text="" class="android.widget.ImageView" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[900,1166][1065,1331]" resource-id="com.autonavi.xmgd.navigator:id/plus_id" instance="0"/><android.widget.ImageView NAF="true" index="1" text="" class="android.widget.ImageView" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[900,1346][1065,1511]" resource-id="com.autonavi.xmgd.navigator:id/sub_id" instance="1"/></android.widget.LinearLayout></android.widget.LinearLayout><android.widget.TextView index="4" text="500m" class="android.widget.TextView" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[903,1514][1077,1578]" resource-id="com.autonavi.xmgd.navigator:id/map_scale_level" instance="2"/><android.widget.LinearLayout index="5" text="" class="android.widget.LinearLayout" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="false" enabled="true" focusable="false" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[15,1596][1065,1761]" resource-id="com.autonavi.xmgd.navigator:id/map_showmap_toolbar" instance="3"><android.widget.Button index="0" text="上一个点" class="android.widget.Button" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="true" enabled="false" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[15,1596][540,1761]" resource-id="com.autonavi.xmgd.navigator:id/map_showmap_pre" instance="0"/><android.widget.Button index="1" text="下一个点" class="android.widget.Button" package="com.autonavi.xmgd.navigator" content-desc="" checkable="false" checked="false" clickable="true" enabled="true" focusable="true" focused="false" scrollable="false" long-clickable="false" password="false" selected="false" bounds="[540,1596][1065,1761]" resource-id="com.autonavi.xmgd.navigator:id/map_showmap_next" instance="1"/></android.widget.LinearLayout></android.widget.RelativeLayout></android.widget.FrameLayout></android.widget.LinearLayout></android.widget.FrameLayout></hierarchy>

@chenhengjie123不知道我上面做的对不对。还是没有解决,请大家帮忙支个招~

#4 楼 @kufire 你的 xml 里面有一些元素带有 NAF="true",而且也没找到看起来是地图里面的元素的节点,所以他们应该是在 NAF="true" 的节点里面,抓取不出来。
如果你确实要点,只能用坐标了。

关于 NAF 请看:
http://stackoverflow.com/questions/25435878/uiautomatorviewer-what-does-naf-stand-for

@chenhengjie123看来没有其他办法了,但还是非常感谢你的回答!

有一个跟楼主差不多的页面,进入时会弹出 toast :invalid_user_scode 使用是高德地图的 API,不知道楼主有没有遇到过

@yxgtianya 没遇到过。刚学 appium 没多久。你在上面情况下,是怎么处理的?

#8 楼 @kufire appium 重签名的问题导致的,目前看来是解决了

uiautomator 获取页面元素中文乱码,机型安卓 5.0.2,请问能解决吗

#1 楼 @chenhengjie123 uiautomator 获取页面元素中文乱码,机型安卓 5.0.2,请问能解决吗

我不清楚,没在 5.0.2 上用过

#13 楼 @zsx10110 这个链接 说的是什么额 ,打不开额 ~

#15 楼 @alisawu

The problem exists in the platform software, so only newer platform has fix for it. I believe the fix was in Android 5.1 Lollipop MR1.

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