【发布时间】:2016-01-09 04:53:57
【问题描述】:
我有这样的线性布局:
现在有些线性布局有子类别,有些没有。类似地,有些有折扣,同样没有。
所以当我做这样的事情时:
List<WebElement> allFieldsInLayout = driver.findElements(By.id("com.flipkart.android:id/product_list_product_item_layout"));
List<WebElement> allTitlesOnCurrentScreen = driver.findElements(By.xpath("//*[@resource-id='com.flipkart.android:id/product_list_product_item_main_text']"));
List<WebElement> allsubTitlesOnCurrentScreen = driver.findElements(By.xpath("//*[@resource-id='com.flipkart.android:id/product_list_product_item_sub_text']"));
List<WebElement> allOfferPricesOnCurrentScreen = driver.findElements(By.xpath("//*[@resource-id='com.flipkart.android:id/product_list_product_item_price']"));
List<WebElement> allListPriceOnCurrentScreen = driver.findElements(By.xpath("//*[@resource-id='com.flipkart.android:id/product_list_product_item_mrp']"));
然后尝试在其中打印文本:
for(int i=0;i<allTitlesOnCurrentScreen.size();i++){
System.out.println("TITLE : "+allTitlesOnCurrentScreen.get(i).getAttribute("text")
+ "SUB TITLE : "+allsubTitlesOnCurrentScreen.get(i).getAttribute("text")
+ "OFFER PRICE : "+allOfferPricesOnCurrentScreen.get(i).getAttribute("text")
+ "LIST PRICE : "+allListPriceOnCurrentScreen.get(i).getAttribute("text")
);
}
我得到了 Array Out of Bound 异常。所以我想是否有可能从这个列表的外部布局中获取所有子字段的资源 ID。我试过这样:
for(int i=0;i<allFieldsInLayout.size();i++){
List<WebElement> allElementsinCurrentLayout = allFieldsInLayout.get(i).findElements(By.xpath("//android.widget.RelativeLayout[@index='2']"));
for(int j=0;j<allElementsinCurrentLayout.size();j++) {
System.out.println("Layout " + allElementsinCurrentLayout.get(j));
}
}
但它给出了例外
Cannot use xpath locator strategy from an element. It can only be used from the root element)
如果我没有相应的子标题或没有折扣,我希望在我的列表中为 NULL。怎么办?
【问题讨论】:
-
为什么要使用这种方法来查找每一项?通过 ID(或名称、标签等)查找每个项目可能更容易,如果您在此处查找,它可以找到不同的方法。
-
@GastonF。你说的是哪种方法?
-
@GastonF。我正在使用 Appium 框架
-
好的,我在您的代码中发现了一个问题,您正在迭代 allTitlesOnCurrentScreen 但尝试获取具有相同索引的值 allListPriceOnCurrentScreen,也许您的代码应该类似于 this
标签: android selenium appium selendroid