Recently in the Learning Automation framework Appium, online to find some API-related information to organize a bit
1.find_element_by_id
find_element_by_id (self, id_):
Finds element within this element's children by ID (position element by ID of Element)
: Args:-id_-ID of child element to locate.
Usage driver. FIND_ELEMENT_BY_ID ("id")
The find_element_by_id method is used for operations that have an ID and can differentiate between controls, typically obtained by means of the Uiautomatorviewer.bat tool in the tools path of the Android SDK,
Use this tool first to ensure that the front environment is configured OK, and then determine the test machine or Android emulator is connected (cmd input adb devices), the following is the use of the Uiautomatorviewer.bat tool
Get Resource-id
code example:
#点击天气应用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 (using XPath to locate elements, see http://www.w3school.com.cn/xpath/for detailed methods)
" //input[@class = ' myelement '] " note:the Base Path would be relative to this element ' s location. This would select the first link under this Element.::myelement.find_elements_by_xpath (".//a") However, this would select the First link on the Page.::myelement.find_elements_by_xpath ("//a") "
Usage Find_element_by_xpath ("//*")
The Find_element_by_xpath method also needs to use the Uiautomatorviewer.bat tool to locate the control, as shown in the right-hand camera control ID is empty, then you need to use XPath to locate the element.
we can use XPath to position the element with text and index, so we can position the camera:
Driver.find_element_by_xpath ("//android.widget.textview[contains (@text, ' camera ')]") Driver.find_element_by_xpath ("//android.widget.textview[contains (@index, 8)]")
such as the gallery that control index and weather temperature control is the same, are index=7, you can only use text or text with index, such as:
Driver.find_element_by_xpath ("//android.widget.textview[contains (@text, ' gallery ')]") Driver.find_element_by_xpath ("//android.widget.textview[contains (@text, ' gallery ') [email protected] = ' 7 '] ")
the code above can be written like this.
#-*-coding=utf-8-*-from appium import Webdriverimport timedesired_caps = {' PlatformName ': ' Android ', ' devicename ': ' 76P 4c15813005463 ', ' platformversion ': ' 5.1 ', #测试apk包名 ' apppackage ': ' Com.huawei.android.launcher ', # Test the apk launcheractivity ' appactivity ': '. Launcher ',} #进入android系统launcherdriver = 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, ' camera ')"). 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) # Two ways to enter the Gallery app and exit from the Launcher main interface #xpath use the text anchor control Driver.find_element_by_xpath ("//android.widget.textview[contains (@ Text, ' Gallery '). Click () time.sleep (2) driver.keyevent (' 3 ') Time.sleep (2) #xpath使用text与index一起定位控件driver. find_element_by _xpath ("//android.widget.textview[contains (@text, ' gallery ') [email protected]= ' 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 "Locating by element name (attribute text)"
property of the element to find.
Usage: driver.find_element_by_name ("name")
The control elements of the Find_elements_by_name method also need to be positioned using the Uiautomatorviewer.bat tool, as the control with text information in the example above can be used directly, as
Driver.find_element_by_xpath ("//android.widget.textview[contains (@text, ' camera ')]") Driver.find_element_by_xpath ("//android.widget.textview[contains (@index, 8)]")
can use
Driver.find_element_by_name (' camera ')
The script is as follows
From appium import Webdriverimport timedesired_caps = {' PlatformName ': ' Android ', ' devicename ': ' 76p4c15813005463 ', ' Platformversion ': ' 5.1 ', #测试apk包名 ' apppackage ': ' Com.huawei.android.launcher ', #测试apk的launcherActivity ' appactivity ' : ‘. Launcher ',} #进入android系统launcherdriver = Webdriver. Remote (' Http://localhost:4723/wd/hub ', Desired_caps) time.sleep (5) #name方法进入相机应用time. Sleep (2) Driver.find_element_ By_name ("Camera"). 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 (elements are positioned by the element class Name property)
class name to search for.
Usage driver. Find_element_by_class_name ("Android.widget.LinearLayout")
Find_element_by_class_name method is actually very impractical, the general control of the class is basically the same, such as the above application control class are Android.widget.TextView, so it is not good to distinguish between elements.
5. Find_element_by_link_text
Find_element_by_link_text (self, Link_text):
Finds element within this element'schildren by visible link text location: args:-link_text-link text S Tring to search for. Usage 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 is positioned by means of an element-visible linking literal, containing all elements of that property: args:-Li Nk_text-link text string to search for. Usage 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. Locate the element by looking up the tag names of the HTML: args:-name-name of HTML tag (eg : H1, A, span) usage 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'a.nav#home '
’
Ten. Find_element_by_ios_uiautomation
find_element_by_ios_uiautomation (Self, uia_string):
Finds an element by UIAutomation in iOS. Find elements via iOS uiautomation: Args:-uia_string-The element name in the IO S uiautomation library:Usage:driver.find_element_by_ios_uiautomation ('. Elements () [1].cells () [2 ]') usage Dr. Find_element_by_ios_uiautomation (' elements ')
find_element_by_accessibility_id.
FIND_ELEMENT_BY_ACCESSIBILITY_ID (self, id):
Finds an element by accessibility ID. Find elements by Accessibility ID: Args: string corresponding to a recursive element search using Theid/Name, the native accessibility options utilize:u SAGE:DRIVER.FIND_ELEMENT_BY_ACCESSIBILITY_ID () Usage driver.find_element_by_accessibility_id (' id ')
Learning Address:
http://blog.csdn.net/bear_w/article/details/50330565
http://blog.csdn.net/bear_w/article/details/50330565
APPIUM API Grooming (python)---element lookup