2016-03-22 36 views
-1

定位的XPath使用AppiumUIAutomatoViewer和Android 5.1不能在Android

你能幫助我,請以定位元素的XPath。我需要從DVR Boxes(客廳,DVR 1在此示例)中獲取文本。我嘗試了不同的組合,但仍然沒有運氣。

我不能使用resource-id,因爲對於RelativeLayout[0]RelativeLayout[1]他們是一樣的。而且我也無法使用文本搜索頁面,因爲文本是動態的(DVR框會改變)。我認爲唯一的解決辦法是XPath的

以下是我所做的:

  1. AndroidElement d = (AndroidElement) 
    driver.findElementByXPath("//android.widget.RelativeLayout[0]/android.widget.RelativeLayout[0]/android.widget.TextView[0]");      
    
  2. AndroidElement d = (AndroidElement) 
    driver.findElementByXPath("//android.widget.RelativeLayout[@index='0']"); 
    System.out.println(d.getText());    
    
  3. AndroidElement d = (AndroidElement) 
    driver.findElementByXPath("//RelativeLayout[0]/TextView[0]"); 
    

但不是我的解決方案,工作至今

enter image description here

+0

嘗試使用'appium inspector'中顯示的XPath作爲屬性部分中的元素 – nullpointer

+0

非常感謝。我使用的是UIAtomatorviewer,並不知道在Appium檢查器中可以看到Full Xpath。 UIAtomatorviewer目前沒有此選項 –

回答

0

我不能使用「資源ID」,因爲RelativeLayout的[0]和 RelativeLayout1他們同

你仍然可以使用resource-ids創建List<WebElement>和使用索引訪問元素。你的情況是這樣的:

List<WebElement> dvrList = driver.findElementsById("<id of the list element"); //creates a list of elements with same id 
String text = dvrList.get(1).getText(); //this gives you text of 2nd element on list (base index 0) 

我認爲唯一的解決辦法是XPath的

除非XPath是正確的訪問元素的方式似乎好

WebElement dvrItem = driver.findElementByXPath("//android.widget.ListView[0]/android.widget.RelativeLayout[0]/android.widget.RelativeLayout[0]/android.widget.TextView[0]"); 
String text = dvrItem.getText(); 

編輯: -

Correct XPath = //android.widget.RelativeLayout[1]/android.widget.R‌​elativeLayout[1]/android.widget.TextView[1] 
+0

找到正確的Xpath:WebElement drv = driver.findElement(By.xpath(「// android.widget.RelativeLayout [1] /android.widget.RelativeLayout [1] /android.widget.TextView [1]「)); drv.getText(); –

+0

希望appium檢查員幫助:) – nullpointer