Android 在复杂布局中使用UiAutomator查找节点

Android 在复杂布局中使用UiAutomator查找节点,android,android-uiautomator,Android,Android Uiautomator,我正在尝试使用UiAutomator来查找嵌套在android应用程序布局中相对较深的ImageView。这是我用来查找它的代码的精简版本: UiObject nextButton = new UiObject(new UiSelector().className(className) .index(2)); nextButton.waitForExists(20000); nextButton

我正在尝试使用UiAutomator来查找嵌套在android应用程序布局中相对较深的ImageView。这是我用来查找它的代码的精简版本:

UiObject nextButton = new UiObject(new UiSelector().className(className)
                                                   .index(2));
nextButton.waitForExists(20000);
nextButton.clickAndWaitForNewWindow();
UiAutomatorViewer的布局如下所示:

(0) FrameLayout
  (0) LinearLayout
    (0) FrameLayout
      (0) RelativeLayout
        (0) LinearLayout
          (0) RelativeLayout
            (0) RelativeLayout
              (0) TextView
          (1) RelativeLayout
            (0) LinearLayout
              (0) TextView
              (1) TextView
            (1) ImageView
          (2) RelativeLayout
            (0) ImageView
            (1) ImageView
          (3) LinearLayout
            (0) LinearLayout
              (0) ImageView
              (1) ImageView
              (2) ImageView       //<----- The one I'm after
            (1) TextView

这里的问题是UiAutomator找不到它。它启动UiWatcher并最终以UiObjectNotFoundException失败。我可以通过在向下的过程中指定一些其他布局来指明方向,从而让它发挥作用,但我希望尽可能避免这样做。有没有更好的方法来查找此ImageView?很遗憾,我使用的是APK 16,所以resourceId是不可能的。

您可以为Framelayout创建一个对象,然后使用Framelayout object for ImageView创建另一个对象,并给出Index2,因为没有其他Index2可用于ImageView。

答案是使用更好的硬件。在速度较慢的硬件上,似乎UiAutomator根本无法在布局中进行足够长的迭代,无论给出多长时间