2017-04-06 65 views
0

我有一个Android应用程序和Appium自动化的可滚动列表(搜索结果),我需要从中选择一个特定的元素。 Appium检查员正在提供诸如index,resource-id和xpath之类的信息。xPath for Android中的上市元素(使用appium自动化)

content-desc: 
type: android.widget.TextView 
text: AMI 
index: 0 
enabled: true 
location: {60, 513} 
size: {81, 61} 
checkable: false 
checked: false 
focusable: false 
clickable: false 
long-clickable: false 
package: com.abc.xyz.debug 
password: false 
resource-id: com.abc.xyz.debug:id/student_label_text 
scrollable: false 
selected: false 

XPath来第一结果(由appium检查员提供)是这样的:

//android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[1] /android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget。 RelativeLayout的[1] /android.widget.LinearLayout [1] /android.widget.LinearLayout [1] /android.widget.TextView [1]

为塞康D转换结果是..

//android.support.v7.widget.RecyclerView[1]/android.widget.FrameLayout[2]/android.widget.FrameLayout[1]/android.widget.LinearLayout [1] /android.widget.LinearLayout [1] /android.widget.LinearLayout [1] /android.widget.FrameLayout [1] /android.widget.RelativeLayout [1] /android.widget.LinearLayout [1] /机器人.widget.LinearLayout [1] /android.widget.TextView [1]

上面的xPaths工作,但我正在寻找更短的版本。我尝试了各种组合,但由于我是新手,所以无法找出解决方案。任何关于这个或任何可以完成这项工作的工具的帮助?

+1

u能提供最后一个TextView.properties在appium inspector中显示的细节 – SaiPawan

+0

@sai更新信息 – TestingWithArif

+0

检查答案 – SaiPawan

回答

1

Xpath的最佳选择通常是在元素的任何属性中找到唯一值。从你的示例值,您可以通过文本价值发现:

driver.findElement(By.xpath("//android.widget.TextView[@text='AMI']")); 

由于XPath是比其他查找策略较慢,你能先取得所有ID匹配,之后检查什么属性值的那些元素有元素:

List<MobileElement> elements = driver.findElements(By.id("com.abc.xyz.debug:id/student_label_text")); 
for (MobileElement element : elements) { 
    if (element.getAttribute("text") == "AMI") { 
     element.click(); 
    } 
} 

请注意,我使用findElements而不是findElement来获取所有匹配元素的列表。

的getAttribute命令的用法是不是非常有据可查。它采取了一些挖掘,以找出所有接受名称的属性:

https://github.com/appium/appium-android-bootstrap/blob/master/bootstrap/src/io/appium/android/bootstrap/AndroidElement.java#L182

https://github.com/appium/appium-android-bootstrap/blob/master/bootstrap/src/io/appium/android/bootstrap/AndroidElement.java#L94

1

而不是XPath,你可以使用正确的id。

使用XPath会影响我们的scripts.We的执行可以使用ID的速度,如下图所示:相比于ID来识别元素

driver.findElement(By.id("student_label_text")) 

的XPath会花费太多时间。

+0

I trie那之前。问题是我有相同的ID多个结果。所以如果我使用它,它会返回多个结果(#可能会有所不同)。 xPath似乎更好,因为我可以根据需要选择第一个或第二个结果。 – TestingWithArif

+1

然后你可以使用driver.findElements(By.id(“com.abc.xyz.debug:id/student_label_text”))。get(0)获得第一个。像这样你可以做,而不是xpath.Because xpath是不建议 – SaiPawan

+0

谢谢。我会尝试并更新。 – TestingWithArif

相关问题