[英]xPath for listing element in android (using appium automation)
我在Android App中有一個可滾動的列表(搜索結果),並且對於Appium自動化,我需要從中選擇一個特定的元素。 Appium檢查器正在提供諸如index,resource-id和xpath之類的信息。
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]
對於第二個結果是..
//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]/android.widget.LinearLayout[1]/android.widget。 TextView [1]
以上xPaths工作,但我正在尋找較短的版本。 我嘗試了各種組合,但是由於我是新手,因此無法找出解決方案。 這方面的任何幫助或任何可以完成這項工作的工具?
可以使用ID right代替XPath。
使用xpath會影響腳本的執行速度。我們可以使用id,如下所示:
driver.findElement(By.id("student_label_text"))
與id相比,XPath會花費很多時間來標識元素。
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命令的用法沒有得到很好的記錄。.花費了一些時間才能找出所有可接受的屬性名稱:
和
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.