appium元素識別方式實戰

github代碼::  https://github.com/w550856163/App_Demo.git  tag: V1.1
Appium Inspector定位工具界面介紹: 
Selecte Elements :根據元素定位 
Swipe By Coordinates:根據座標滑動頁面 
Tap By Coordinates:根據座標點擊操作 
Back:返回 
Refresh Source & Screenshot:刷新界面與截圖 
Start Recording:錄製按鈕,能自動生成自動化腳本 
Search for element:在App Source中搜索元素 
Copy XML Source to Clipboard :複製XML源代碼到 
Quit Session & Close Inspector:退出會話或結束 
Source:源代碼顯示區域 
Actions: 
Selected Element:被選中元素信息顯示
 
 

 

Appium元素定位方法介紹及應用: 
通過上面兩種工具,可以獲取元素的相關信息;在appium中提供了一系列 
的元素定位API,通過在這些API中輸入指定的元素信息,就能完成元素定位, 
定位元素後就進行對元素進行自動操作。 
基本元素定位API如下: 
1、ID定位(取resource-id、id、name) 
driver.find_element_by_id('') 
2、class定位(取class) 
driver.find_element_by_class_name('') 
3、AccessibilityId定位(取content-desc) 
driver.find_element_by_accessibility_id('') 
備註:其中name方式(取text)已不支持
 

 

 

2、使用 屬性 定位: 
text屬性: 
driver.find_element_by_xpath('//類名[@text=text值]') 
resource-id屬性: 
driver.find_element_by_xpath('//類名[@resource-id=屬性值]') 
content-desc屬性: 
driver.find_element_by_xpath('//類名[@content-desc=屬性值]') 
class屬性: 
driver.find_element_by_xpath('//類名') 
多屬性:and 表示並且 or表示或者 
driver.find_element_by_xpath('//類名[@屬性名=屬性值 and/or @屬性名=屬性 
值]') 
備註: 
1、所有元素的屬性都可以進行定位,但必須該屬性的值是界面上唯一的 
2、xpath支持 * 代表任意個字符 ,比如find_element_by_xpath('//*[@text=text 
值]') ,這種用法容易同時識別多個元素
Xpath定位方法: 
3、使用 部分屬性內容 定位: 
當元素的屬性值過長或者元素屬性值內容中存在動態變化的情況下,可以使 
用該方法。 
起始位置匹配:starts-with() 
driver.find_element_by_xpath('//類名[starts-with(@屬性名,部分屬性值)]') 
包含匹配:contains() 
driver.find_element_by_xpath('//類名[contains(@屬性名,部分屬性值)]') 
末尾位置匹配:ends-with() 
driver.find_element_by_xpath('//類名[ends-with(@屬性名,部分屬性值)]')

 

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