Appium + Python-element positioning (common positioning)

  The environment construction and entry examples have been introduced earlier. In order to learn more deeply, you need to be familiar with various element positioning methods in order to apply them to specific example scenarios. This chapter mainly covers the content;

  • find_element_by_id

  • find_element_by_class_name

  • find_element_by_accessibility_id
  • find_element_by_xpath

  • find_element_by_android_uiautomator

  • driver.tap

First, locate the element by id (find_element_by_id)

  The corresponding field is the reduce-id. Generally, the ID is used to locate the priority, but the id is sometimes not unique. Before use, it is necessary to determine whether the id is unique.
# 
idGo to the homepage driver.find_element_by_id ( " com.taobao.taobao:id/iv_image " ) .click ()  

Second, locate the element by ClassName (find_element_by_class_name)

   The corresponding field is class, the clsaa attribute determines the type of interface element, if you want to find a certain type of interface element, and there is only one type of interface element in the current interface, you can use this method, but the frequency of by_class_name is not very high , Because it is easy to appear that the class of multiple elements is the same one.

# 
classLocate the input box driver.find_element_by_class_name ( " android.widget.EditText " ) .click ()

3. Locate elements by content-desc (find_element_by_accessibility_id)

  The corresponding field is content-desc. The content-desc attribute is used to describe the role of the element. If the content-desc of the interface element to be queried is unique in the current interface, you can use this method. In most cases, this field is air

# content- 
descLocate the search button driver.find_element_by_accessibility_id ( ' Search ' ) .click ()  

Fourth, locate the element by xpath (find_element_by_xpath)

  When none of the above methods can locate a specific element, you can use the xpath method to locate the element

1. If the element text is unique, it can be positioned by the text text

# When the text page is determined that only 
driver.find_element_by_xpath ( " // * [@ text = 'Evaluation'] " ) .click ()

2. If the element id is unique, you can also locate by the id attribute

# id定位确定
driver.find_element_by_xpath("//*[@resource-id='com.taobao.taobao:id/confirm_text']").click() 

3. Joint positioning by text and id

# The combination of id and text locates the evaluation page 
driver.find_element_by_xpth ( " //*[@resource-id='com.taobao.taobao:id/taodetail_nav_bar_tab_text'][@text='review '] " ) .click ()

4. If the class attribute is unique, it can also be located by the class attribute, there are two methods

# The first method 
driver.find_element_by_xpath ( " //android.widget.EditText " ) .click () 

# The second method 
driver.find_element_by_xpath ( " //*[@class='android.widget.EditText '] " ) .click ()

5. Positioning through the content-desc attribute, contains is the positioning method of fuzzy matching. For an element, the id or text is not fixed, but part of it is fixed, which can be fuzzy matching.

# 1st: Match text, fuzzy positioning to baby evaluation 
driver.find_element_by_xpath ( " // * [contains (@text, '宝贝 Evaluation')] " ) .click () 

# 2nd: match content-desc, fuzzy positioning a return 
driver.find_element_by_xpath ( " // * [the contains (Content-desc @, 'return')] " ) .click () 

# No. 3: matching class, the fuzzy input to the positioning block 
driver.find_element_by_xpath ( " // * [the contains (@class, 'android.widget.EditText')] " ) .click () 

# 4 species: matching the id, to locate the fuzzy input box 
driver.find_element_by_xpath ( " // * [the contains (@ resource- id, 'com.taobao.taobao:id/searchEdit')] " ) .click ()

6. Combined positioning

If an element has 2 attributes, you can also match 2 attributes at the same time through xpath. The attributes of text, resource-id, class, index, content-desc can be positioned in any combination.

# id and class attribute positioning search box 
id_class = ' //android.widget.EditText[@resource-id="com.taobao.taobao:id/home_searchedit "] ' 
driver.find_element_by_xpath (id_class) .click () 
time.sleep ( 3 ) 
driver.back () 

# text and index attribute positioning login / registration 
desc_class = ' // * [@ text = "registration / login" and @ index = "1"] ' 
driver.find_element_by_xpath (desc_class) .click ( ) 
time.sleep ( 3 ) 

# Class and text attribute positioning input phone number 
class_text = ' //android.widget.EditText[@text="Please enter the phone number "] ' 
driver.find_element_by_xpath (class_text) .send_keys ( "512200893")
time.sleep(3)

# class和desc  定位帮助
id_desc = '//*[contains(@resource-id, "aliuser_menu_item_help") and @content-desc="帮助"]'
driver.find_element_by_xpath(id_desc).click()

7. Hierarchical positioning

If an element has nothing but the class attribute (the class attribute will definitely exist), it has no other attributes. In this case, the above method is not applicable. At this time, you can find the parent element and locate the son through the father

# If the next parent element, a plurality of the same class son time, the index can correspond to fetch the first few xpath, starting from 1 xpath number 
loc_class = ' // * [@ Resource-ID = "COM. taobao.taobao:id/libsf_srp_header_list_recycler"]/android.widget.RelativeLayout[2] ' 
driver.find_element_by_xpath (loc_class) .click ()

Conversely, the father can be located by the son

# Through the sub-elements parent element positioned 
# Method a: .. 
sun_fa1 = ' //*[@resource-id="com.taobao.taobao:id/tv_scan_text"]/ .. ' 
C = driver.find_element_by_xpath (sun_fa1). the tag_name
 Print (C) 

# method II :: * parent 
sun_fa2 = ' //*[@resource-id="com.taobao.taobao:id/tv_scan_text"]/parent::* ' 
D = driver.find_element_by_xpath (sun_fa1) .tag_name
 Print (D) 

# method three :: android.widget.LinearLayout parent 
sun_fa3 = ' //*[@resource-id="com.taobao.taobao:id/tv_scan_text"]/parent::android.widget.LinearLayout '
is= driver.find_element_by_xpath(sun_fa1).tag_name
print(e)

Through the child element, first find the parent element, and then find the child element under the parent element, you can also locate the sibling element

# Sibling 
xiongdi = ' //*[@resource-id="com.taobao.taobao:id/bar_search"]/../android.widget.RelativeLayout ' 
X = driver.find_element_by_xpath (xiongdi) .tag_name
 Print (X )

Through the hierarchical relationship, looking down layer by layer, you can also locate the grandson element through the grandpa element

# Grandfather first element FrameLayout --- --- FrameLayout son grandchildren the TextView 
X = ' //android.widget.FrameLayout/android.widget.LinearLayout[1]/android.widget.TextView ' 
T = driver.find_elements_by_xpath (X )
 Print (len (T))
 # print text information 
Print (T [0] .text

Fifth, locate elements through AndroidUiAutomator (find_element_by_android_uiautomator)

  AndroidUIAutomator is a powerful element positioning method, it is to find elements through the Android UIAutomator class library, you can choose id, name, className as the incoming string

1. Positioning by id

# By targeting product id 
ui_id = ' new new UiSelector (). The resourceId ( "com.taobao.taobao:id/goodsimage") ' 
driver.find_element_by_android_uiautomator (ui_id) .click ()

2. Positioning by text

# By text positioning ADD TO CART button 
UI_TEXT = ' new new UiSelector (). Text ( "Add to Cart") ' 
driver.find_element_by_android_uiautomator (UI_TEXT) .click ()

3. Positioning by class

# classname plural positioning, positioning a product by class 
ui_class = ' new UiSelector (). className ("android.widget.RelativeLayout") '  
driver.find_element_by_android_uiautomator (ui_class [1]). click ()

 

4. Combination positioning, generally combined with three attributes of id, class and text

# 第 1 种 : The combination of id and text is located in the shopping cart 
id_text = ' resourceId ("com.taobao.taobao:id/detail_main_sys_button"). Text ("add to cart") ' 
driver.find_element_by_android_uiautomator (id_text) .click ( ) 

# of two kinds: class and target text composition Add cart 
class_text = ' className ( "android.widget.TextView") text ( "Add to Shopping cart"). ' 
driver.find_element_by_android_uiautomator (class_text) .click ()

. 5, Description , is positioned with properties contenet-des

# Targeting return through-desc Content 
ui_content_desc = ' new new UiSelector (). Description ( "return") '  
driver.find_element_by_android_uiautomator (ui_content_desc)

6. Parent-child positioning childSelector, sometimes it is not possible to directly locate an element, but its parent element is well positioned, at this time, first locate the parent element, find the son through the parent element

# Parent-child relationship childSelector 
son = ' resourceId ("com.baidu.yuedu: id / rl_tabs"). ChildSelector (text ("novel")) ' 
driver.find_element_by_android_uiautomator (son) .click ()

6. Brother positioning fromParent, sometimes the parent element is not easy to locate, but the sibling element adjacent to him is well positioned, at this time, the child element under the same parent element can be found through the sibling element

#兄弟关系fromParent
brother = 'resourceId("com.baidu.yuedu:id/lefttitle").fromParent(text("图书"))'
driver.find_element_by_android_uiautomator(brother).click()

Six, positioning elements by tap (driver.tap)

  tap (self, positions, duration = None): Simulate finger tapping (up to five fingers), and set the length of time (ms)

-positions-list type, where the objects are tuples, up to five. Such as: [(100, 20), (100, 60)] 
    
-duration-duration, in milliseconds, such as: 500
# 
Tap coordinate to locate the motion driver.tap ([(720,72), (1080,216)], 500)

 

Among the above positioning elements, the hierarchical, parental, and sibling positioning have not yet been used. The usage in the article is to excerpt other blogs. Later, when they encounter suitable scenes, they will come back to post.

 




Guess you like

Origin www.cnblogs.com/Chilam007/p/12728316.html