最近在學習自動化框架appium,網上找一些API相關資料整理了一下css
1.find_element_by_idhtml
find_element_by_id(self, id_):python
Finds element within this element's children by ID(經過元素的ID定位元素)android
:Args: - id_ - ID of child element to locate.ios
用法 driver. find_element_by_id(「id」)web
find_element_by_id方法,是對於那些有id並且能夠互相區分的控件的操做使用,通常經過android sdk 的tools路徑下的uiautomatorviewer.bat自帶工具來獲取,app
使用這個工具首先要確保前面環境配置ok,而後肯定測試機器或android模擬器處於鏈接狀態(cmd上輸入adb devices),下面是使用uiautomatorviewer.bat工具框架
獲取resource-id工具
代碼舉例:學習
#點擊天氣應用 driver.find_element_by_id('com.huawei.android.totemweather:id/mulan_widget_currentweather_smallicon').click()
2.find_element_by_xpath
find_element_by_xpath(self, xpath):
Finds element by xpath(經過Xpath定位元素,詳細方法可參閱http://www.w3school.com.cn/xpath/ )
``` :Args: xpath - xpath of element to locate. "//input[@class='myelement']" Note: The base path will be relative to this element's location. This will select the first link under this element. :: myelement.find_elements_by_xpath(".//a") However, this will select the first link on the page. :: myelement.find_elements_by_xpath("//a") ```
用法 find_element_by_xpath(「//*」)
find_element_by_xpath方法也須要使用uiautomatorviewer.bat工具來定位控件,以下圖所示右邊相機控件id爲空,這時就須要使用xpath來定位元素了,
咱們能夠使用xpath用text和index來定位元素,能夠這樣定位相機:
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'相機')]") driver.find_element_by_xpath("//android.widget.TextView[contains(@index,8)]")
以下圖的圖庫那個控件index和天氣溫度控件是相同的,都是index=7,就只能用text或text與index結合了,如:
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'圖庫')]") driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'圖庫')and@index='7']")
上面的代碼就能夠這樣寫
# -*-coding=utf-8 -*-
from appium import webdriver
import time
desired_caps = {
'platformName' : 'Android',
'deviceName' : '76P4C15813005463',
'platformVersion' : '5.1',
#測試apk包名
'appPackage' : 'com.huawei.android.launcher',
#測試apk的launcherActivity
'appActivity' : '.Launcher',
}
#進入android系統launcher
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
time.sleep(5)
#從launcher主界面進入相機應用並退出的兩種方法
driver.keyevent('3')
#xpath使用text定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'相機')]").click()
time.sleep(2)
driver.keyevent('3')
time.sleep(2)
#xpath使用index定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@index,8)]").click()
time.sleep(2)
driver.keyevent('3')
time.sleep(2)
#從launcher主界面進入圖庫應用並退出的兩種方法
#xpath使用text定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'圖庫').click()
time.sleep(2)
driver.keyevent('3')
time.sleep(2)
#xpath使用text與index一塊兒定位控件
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'圖庫')and@index='7']").click()
time.sleep(5)
driver.keyevent('3')
time.sleep(2)
driver.quit()
3. find_element_by_name
find_element_by_name(self, name):
Finds element within this element's children by name【經過元素Name定位(元素的名稱屬性text)】
:Args: - name - name property of the element to find.
用法: driver.find_element_by_name(「name」)
find_elements_by_name方法的控件元素也須要使用uiautomatorviewer.bat工具來定位,如上面例子中帶有text信息的控件就能夠直接使用,如
driver.find_element_by_xpath("//android.widget.TextView[contains(@text,'相機')]") driver.find_element_by_xpath("//android.widget.TextView[contains(@index,8)]")
能夠使用
driver.find_element_by_name('相機')
腳本以下
from appium import webdriver
import time
desired_caps = {
'platformName' : 'Android',
'deviceName' : '76P4C15813005463',
'platformVersion' : '5.1',
#測試apk包名
'appPackage' : 'com.huawei.android.launcher',
#測試apk的launcherActivity
'appActivity' : '.Launcher',
}
#進入android系統launcher
driver = webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
time.sleep(5)
#name方法進入相機應用
time.sleep(2)
driver.find_element_by_name("相機").click()
time.sleep(5)
driver.quit()
4. find_element_by_class_name
find_element_by_class_name(self, name):
Finds element within this element's children by class name(經過元素class name屬性定位元素 )
:Args: - name - class name to search for.
用法 driver. find_element_by_class_name(「android.widget.LinearLayout」)
find_element_by_class_name方法其實很不實用,通常控件的class基本相同,以下圖上面的應用控件class都是android.widget.TextView,因此很差區分元素。
5. find_element_by_link_text
find_element_by_link_text(self, link_text):
Finds element within this element's children by visible link text. 經過元素可見連接文本定位 :Args: - link_text - Link text string to search for. 用法 driver.find_element_by_link_text(「text」)
6. find_elements_by_link_text
find_element_by_link_text(self, link_text):
Finds a list of elements within this element's children by visible link text 經過元素可見連接文本定位,含有該屬性的全部元素 :Args: - link_text - Link text string to search for. 用法 driver.find_elements_by_link_text(「text」)
7. find_element_by_partial_link_text
find_element_by_partial_link_text(self, link_text):
8. find_element_by_tag_name
find_element_by_tag_name(self, name):
Finds element within this element's children by tag name. 經過查找html的標籤名稱定位元素 :Args: - name - name of html tag (eg: h1, a, span) 用法 driver.find_element_by_tag_name(「name」)
9. find_element_by_css_selector
find_element_by_css_selector(self, css_selector):
‘Finds element within this element's children by CSS selector. 經過CSS選擇器定位元素 :Args: - css_selector - CSS selctor string, ex: 'a.nav#home'
’
10. find_element_by_ios_uiautomation
find_element_by_ios_uiautomation(self, uia_string):
Finds an element by uiautomation in iOS. 經過iOS uiautomation查找元素 :Args: - uia_string - The element name in the iOS UIAutomation library :Usage: driver.find_element_by_ios_uiautomation('.elements()[1].cells()[2]') 用法dr. find_element_by_ios_uiautomation(‘elements’)
11. find_element_by_accessibility_id
find_element_by_accessibility_id(self, id):
Finds an element by accessibility id. 經過accessibility id查找元素 :Args: - id - a string corresponding to a recursive element search using the Id/Name that the native Accessibility options utilize :Usage: driver.find_element_by_accessibility_id() 用法driver.find_element_by_accessibility_id(‘id’)
學習地址:
http://blog.csdn.net/bear_w/article/details/50330565
http://blog.csdn.net/bear_w/article/details/50330565