2016-07-05 32 views
0

我試圖用xpath來解決這個問題,但我打開了另一個解決方案。如何從文檔中獲取Appium子元素時,根據電話類型(Button/TextView),子元素是不同的

在不同的電話上,文檔的創建方式不同。在LinearLayout裏面的孩子是三星上的Button,LG上的TextView

我想知道是否有簡單的方法來獲得一個或另一個。我試過用xpath來檢查Button。如果它存在,那麼它的samsung並點擊它。問題是,Appium只是試圖找到Button掛起。我想它會看起來繼續前進。

我可以設置代碼來獲得父級,然後通過類找到孩子並採取第二個索引,但我想知道是否有更好的方法。

有沒有辦法做xpath來處理孩子而不指定其名稱/類型?

By.xpath("//android.widget.LinearLayout[@index='0'][1]") 

下面是當前的代碼:

if (!StringUtility.isEmptyString(driver.findElement(By.xpath("//android.widget.Button[@index='0']")).getAttribute("name"))) { 
driver.findElement(By.xpath("//android.widget.Button[@index='0']")).click(); 
} else { // LG or Other 
    driver.findElement(By.xpath("//android.widget.TextView[@index='0']")).click(); 
} 

LG Phone

Samsung Phone

Appium日誌:

> info: [debug] Pushing command to appium work queue: ["find",{"strategy":"xpath","selector":"//android.widget.Button[@index='0']","context":"","multiple":false}] 
> info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"find","params":{"strategy":"xpath","selector":"//android.widget.Button[@index='0']","context":"","multiple":false}} 
> info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION 
> info: [debug] [BOOTSTRAP] [debug] Got command action: find 
> info: [debug] [BOOTSTRAP] [debug] Finding //android.widget.Button[@index='0'] using XPATH with the contextId: multiple: false 
> info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":7,"value":"Could not find an element using supplied strategy. "} 
> info: [debug] Waited for 10987ms so far 

回答

0

可以動態使用功能

if(capabilities.getCapability("deviceName")).equals("Samsung") 
xpath = "//android.widget.Button[@index='0']" 
else 
xpath = "//android.widget.TextView[@index='0']" 

driver.findElement(By.xpath(xpath)).click(); 
+0

問題創造XPath是你必須做的,對於每個設備名稱。也許這將在未來使用更多設備。我不喜歡的唯一問題是deviceName不是三星或LG的deviceName = LGH820f9798a22。 我認爲的另一種方式是當它查找Button並重試TextView時捕獲異常。 – ALM

相關問題