Android 自動化之元素定位 xpath

1.通過xpath定位元素

uiautomatorviewer  獲取元素路徑

2. 像圖片1的元素路徑 爲:

find_element_by_xpath("//android.support.v4.view.ViewPager/android.widget.Linearlayout/android.widget.LinearLayout[3]/android.widget.Button[1]")

從外往裏一層一層的剝,最終找到要的元素。

注意:xpath路徑不能從最外層的整個頁面開始,那樣會報錯。要從包含該控件的最上層的class開始寫。

什麼是包含該空間的最上層元素呢? 像這裏是:android.support.v4.view.ViewPager

我們點擊 android.support.v4.view.ViewPager 就發現 實際就是整個鍵盤區域

andriod.support.v4.view.ViewPager

元素的定位貌似和我們用uiautomatorviewer獲取到的不一致。

android.widget.LinearLayout[3]/android.widget.Button[1]  而不是 android.widget.LinearLayout[2]/android.widget.Button[0]

筆者認爲要 定位第三個android.widget.Linearlayout中的第一個button.

有一種說法xpath定位元素是從1開始 而不是從0開始

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章