JAVA编写的Appium测试中LinearLayout元素列表索引的奇怪行为

JAVA编写的Appium测试中LinearLayout元素列表索引的奇怪行为,java,android,appium,ui-automation,mobile-application,Java,Android,Appium,Ui Automation,Mobile Application,我正在用Appium测试一个本地Android移动应用程序,测试是用Java编写的 在下面的代码中,我试图在应用程序的菜单列表中搜索某个项目,然后单击。菜单共有4项,但listSize被读取为35项,并且读取值因执行而异。此外,如果我单击,例如menuList.get(0).click(),则在执行过程中,它是第二个单击的项目,而不是第一个项目 打印出下面列出的结果 Appium版本1.4.13.1,JDK版本1.8.0_66。不确定是否有其他相关信息,请询问是否有疑问 List<WebE

我正在用Appium测试一个本地Android移动应用程序,测试是用Java编写的

在下面的代码中,我试图在应用程序的菜单列表中搜索某个项目,然后单击。菜单共有4项,但
listSize
被读取为35项,并且读取值因执行而异。此外,如果我单击,例如
menuList.get(0).click()
,则在执行过程中,它是第二个单击的项目,而不是第一个项目

打印出下面列出的结果

Appium版本1.4.13.1,JDK版本1.8.0_66。不确定是否有其他相关信息,请询问是否有疑问

List<WebElement> menuList = driver.findElementsByClassName("android.widget.LinearLayout");
    int listSize = menuList.size();
    int index = 0;
    Boolean menuFound = false;
    while(!menuFound && index < listSize)
    {
        String label = menuList.get(index).findElement(By.id("no.ruter.reise.qa:id/label")).getText();
        System.out.printf("%d of %d, %s\n", index, listSize, label);
        if (label.equals(menuItem)){
            menuList.get(index).click();
            System.out.printf("\t%s %s\n", menuItem, "click");
            menuFound = true;
        }
        index++;
    }
您使用的是“android.widget.LinearLayout”类,这是一个常见的类,您的应用程序有35个布局,其类名为“android.widget.LinearLayout”

这些布局可以是嵌套格式。您需要为菜单创建定位器,如果您的所有菜单定位器的id为“no.ruter.reise.qa:id/label”,则可以使用以下代码:

List<WebElement> menuList = driver.findElementsByClassName("no.ruter.reise.qa:id/label");
int listSize = menuList.size();
int index = 0;
Boolean menuFound = false;
for(WebElement menu : menuList)
{
    String label = menu.getText();
    System.out.printf("%d of, %s\n", index,label);
    if (label.equals(menuItem)){
        menu.click();
        System.out.printf("\t%s %s\n", menuItem, "click");
        menuFound = true;
    }
    index++;
} 
List menuList=driver.findElementsByClassName(“no.ruter.reise.qa:id/label”);
int listSize=menuList.size();
int指数=0;
布尔menuFound=false;
用于(WebElement菜单:菜单列表)
{
字符串标签=menu.getText();
System.out.printf(“%d,共%s\n”,索引,标签);
if(label.equals(menuItem)){
菜单。单击();
System.out.printf(“\t%s%s\n”,菜单项,“单击”);
menuFound=真;
}
索引++;
} 
您使用的是“android.widget.LinearLayout”类,该类很常见,并且您的应用程序有35个布局,其类名为“android.widget.LinearLayout”

这些布局可以是嵌套格式。您需要为菜单创建定位器,如果您的所有菜单定位器的id为“no.ruter.reise.qa:id/label”,则可以使用以下代码:

List<WebElement> menuList = driver.findElementsByClassName("no.ruter.reise.qa:id/label");
int listSize = menuList.size();
int index = 0;
Boolean menuFound = false;
for(WebElement menu : menuList)
{
    String label = menu.getText();
    System.out.printf("%d of, %s\n", index,label);
    if (label.equals(menuItem)){
        menu.click();
        System.out.printf("\t%s %s\n", menuItem, "click");
        menuFound = true;
    }
    index++;
} 
List menuList=driver.findElementsByClassName(“no.ruter.reise.qa:id/label”);
int listSize=menuList.size();
int指数=0;
布尔menuFound=false;
用于(WebElement菜单:菜单列表)
{
字符串标签=menu.getText();
System.out.printf(“%d,共%s\n”,索引,标签);
if(label.equals(menuItem)){
菜单。单击();
System.out.printf(“\t%s%s\n”,菜单项,“单击”);
menuFound=真;
}
索引++;
} 

感谢您的回复。是的,仔细看之后,我确实发现应用程序有很多布局,类名为“android.widget.LinearLayout”。但是,菜单项self没有id,但包含一个id为
no.ruter.reise.qa:idlabel
的TextView元素。我尝试以这种方式定位菜单列表,而不是
driver.findElementById(“no.ruter.reise.qa:id/drawer\u navigation\u list”).findElements(By.className(“android.widget.LinearLayout”)成功了。感谢您的回复。是的,仔细看之后,我确实发现应用程序有很多布局,类名为“android.widget.LinearLayout”。但是,菜单项self没有id,但包含一个id为
no.ruter.reise.qa:idlabel
的TextView元素。我尝试以这种方式定位菜单列表,而不是
driver.findElementById(“no.ruter.reise.qa:id/drawer\u navigation\u list”).findElements(By.className(“android.widget.LinearLayout”)成功了。