2016-02-18 170 views
0

我已經將id與元素關聯,但是嘗試使用findElement(By.id())查找時,它不起作用。在Appium中通過id查找元素

另外我已經通過了幾個博客,像在這裏Can we find element by ID in appium一樣的問題,但我看到在那裏的資源ID有前綴包名和:id。在我的情況下,這些沒有關聯。元素細節的屏幕截圖如下: enter image description here

我已經使用下面的代碼通過id來定位元素。

- driver.findElement(By.id("loginHome")); 
- driver.findElement(By.id("com.packagename:id/loginHome")); 
- driver.findElement(By.id("android:id/loginHome")); 
- driver.findElement(By.xpath(//*[@id='loginHome'])); 

但上述代碼片段都沒有工作。

有人可以幫助我解決這個問題!

日Thnx提前...

+0

你好嗎'NoSuchElementException'所有的試過的方法?你也可以試試'driver.findElement(By.name(「。Login」));' – nullpointer

+0

是的,我得到了NoSuchElementException。 嘗試使用driver.findElement(By.name(「。Login」));但同樣的錯誤。這是因爲登錄前看到的點(。)實際上並不是一個點,而是一個統一的字符,因此無法繼續進行此操作。 – harshaarchak

回答

1

不完全回答你的問題,而是提供了一種替代方法爲你完成測試。

我按照此鏈接https://github.com/appium/appium-dot-app它的工作原理。

你不需要編碼所有東西。該應用程序提供了一些簡單的方法來記錄您的點擊和手勢。

例如,當我點擊某個東西時,它會自動打印出腳本。 wd.find_element_by_xpath(「// android.view.View [1] /android.widget.FrameLayout [2] /android.widget.LinearLayout [1] /android.widget.RelativeLayout [2] /android.widget.RelativeLayout [ 1]「)。點擊()

而您只需選擇自己喜歡的編程語言並在完成測試後保存它。

我必須指出的一件事是,刷卡功能已損壞。使用以下代碼執行輕掃手勢

wd.swipe(421, 424, 70, 424, 500) 
+0

嗨,我在Windows上使用Appium,當您點擊或點擊任何元素時,它不會打印任何東西。然而,我提出了自定義xpath,因爲你已經提到過,它工作正常。 這是代碼:driver.findElement(By.xpath(「// android.webkit。WebView/descendant :: android.widget.Button [@ index = 4]「)); 但是這不是我正在尋找的,因爲在少數情況下,真的不可能使用絕對路徑一直導航到元素 因此,我想用上面代碼片段中顯示的id來嘗試,但這並不能幫助我找到屏幕上的元素。 – harshaarchak

+0

我正在使用Mac來執行這些測試,但是我記得他們提供兩種平臺的Inspector/Recorder。https://github.com/appium/appium-app-dot-app/blob/master/README-files/images/inspector.png。這可能會更好,因爲你不需要來編碼這些xpath,它們的長度很長:/ – VoidExplorer

+0

不幸的是,Mac上的Inspector的記錄器和增加的功能在Windows版上不可用!:( – harshaarchak

0

您也可以嘗試其他選項。試試這個:

//add some wait command before the element 
Thread.sleep(2000); 
driver.findElementsByXPath("//*[@class='android.widget.Button' and @index='4']"); 
0

嘗試添加明確的睡眠頁面加載之前單擊我們的ID。

0

先試着等待特定元素

MobileElement expelement=(MobileElement) (new WebDriverWait(driver, 15)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//android.widget.Button[@index='4']"))); 

然後嘗試做點擊(或你該做的活動)

driver.findElement(By.xpath("//android.widget.Button[@index='4']")).click();