Robotium 求大神支招,获取相同 id 的控件。

张可 · 2015年01月19日 · 最后由 木月 回复于 2015年11月10日 · 1718 次阅读


菜鸟一枚,被相同 id 的控件搞的很无奈,这种 id 相同的不知道怎么定位。还有很多类似的情况,id 相同,text 也相同。

共收到 18 条回复 时间 点赞

RObotium 支持点击多个 ID 相同的 view 对象中的其中一个啊,
solo.clickOnView(solo.getView(id, Instance)) 就可以实现

方案上面给出了..
实际上就是这样子
Java:

List<WebElement> textList = driver.findElementsByAndroidUIAutomator(
                "new UiSelector().resourceId(\"com.xxx.xx:id/txtid\")");
        String text = textList.get(5).getText();

Python:

textList = driver.find_element_by_android_uiautomator("new UiSelector().resourceId(\"com.xxx.xx:id/txtid\")")
for text in textList:
     print text.text
张可 #16 · 2015年01月19日 Author

#2 楼 @xuxu 能稍微具体一点吗,听不太明白,是得到一个 TextView 的集合再在里面定位吗?

list list=driver.find elements by id ("")

也可以获取父类,再通过父类去获取子类

看一下 SOLO 的 API。
android.view.View getView(String id, int index)
Returns a View matching the specified resource id and index.
元素个数及索引可以取 GRIDVIEW 的 COUNT 来计数。

张可 #11 · 2015年01月20日 Author

#7 楼 @75281920 这个感觉和 UI AUTOMATOR 获取的信息都差不多啊

如果中文能识别出来的话 就用 Ui selector 去定位

张可 #18 · 2015年01月19日 Author

换了头像帖子列表的头像不更新吗...

用 instance

这个问题我也一直很困扰,请问楼主解决了吗?

lz 可参照下:

View view = null;
ListView listview = solo.getCurrentViews(ListView.class,view).get(0);
LinearLayout LL = (LinearLayout) listview.getChildAt(2);
TextView  tx = (TextView) LL.findViewById(R.id.text);

在 LinearLayout 那指定第几个。。

张可 #14 · 2015年03月02日 Author

#12 楼 @miumiu 解决了,用父控件来进行定位

@kesha0 代码贴了学习一下吧,LZ

张可 #16 · 2015年03月03日 Author

ListView gridview=(ListView) this.getViewId("gridview");
LinearLayout temp=(LinearLayout) gridview.getChildAt(0);
RelativeLayout temp1=(RelativeLayout) temp.getChildAt(1);
solo.clickOnView(temp1.getChildAt(1));

这个只是例子,父控件的 ID 是唯一的,通过父控件的 getChildAt 方法来定位

View view = null;
GridView gv = solo.getCurrentViews(GridView.class,view).get(0);
RelativeLayout RL = (RelativeLayout) gv.getChildAt(4); //这里就报错了
solo.clickOnView(RL.getChildAt(1));

#13 楼 @sziitash

列表或者集合

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