问题
I write an automation testing software and have one problem now. In my actual native app I have a ListView like this here:
- ListView
- LinearLayout
- LinearLayout
- LinearLayout
- TextView
- LinearLayout
- LinearLayout
- LinearLayout
- LinearLayout
- LinearLayout
- LinearLayout
- ...
- LinearLayout
... and more.
What I need and want is to get all one level lower LinearLayout (iterative ones, visible and non visible elements) from ListView:
ListView : {LinearLayout, LinearLayout, ...}
With that code I get all childs from ListView, but its to much for me:
listViewChildElements = "[...]android.widget.ListView[1]//android.widget.LinearLayout";
app.getDriver().findElements(By.xpath(listViewChildElements));
In another post I see something with 'count', but I don't understand it.
Thanks for your help and nice to meet you.
With the help of @har07 I write one function, not best or finest solution and not finally, but the 1st step in the right lane:
public static List<String> getAllViewElements(App app){
int i = 0;
List<String> writtenElementsByText = new ArrayList<String>();
while(true){
if(!writtenElementsByText.isEmpty()){
String recentLastItem = writtenElementsByText.get(writtenElementsByText.size()-1);
if(recentLastItem.equals("Alle Themen"))
break;
else
app.getDriver().scrollTo(recentLastItem);
}
List<WebElement> tmp = app.getDriver().findElements(By.xpath(listViewChildElements));
if(writtenElementsByText.isEmpty())
for(WebElement e: tmp){
writtenElementsByText.add(getTextFromElement(e));
}
else
for(WebElement e: tmp){
String activualElemenByText = getTextFromElement(e);
if(!writtenElementsByText.contains(activualElemenByText))
writtenElementsByText.add(activualElemenByText);
}
}
System.out.println(writtenElementsByText.size());
for(String e: writtenElementsByText){
System.out.println("\t-\t"+e);
}
return writtenElementsByText;
}
public static String getTextFromElement(WebElement element){
return element.findElement(By.id(listViewChildElementTextID)).getText();
}
回答1:
If I understand the problem correctly, you want to use single slash (/
) instead of double to get direct child element from a parent element :
listViewChildElements = "[...]android.widget.ListView[1]/android.widget.LinearLayout";
^
notice that only one slash being
used above
With that change, the XPath should now return LinearLayout
that is direct child of the ListView[1]
.
回答2:
I hope the following piece of code helps :
WebElement listViewElement = driver.findElement(<your ListView locator>);
List<WebElement> listLinearLayoutChildren = listViewElement.findElements(By.className("android.widget.LinearLayout")); //All the linear layouts under ListView in one list
listLinearLayoutChildren.get(index).<action>; //index of Nth linearlayout starting from 0 and <action> you want to perform
回答3:
One of the simplest way to do by setting Tag to each linearLayout
.
Now access linearlayout by view.findViewWithTag(index)
来源:https://stackoverflow.com/questions/35698359/get-all-child-notes-only-one-level-lower-from-parent-object-in-appium-android