Detailed positioning of Appium xpath

Have said before appium also to webdriver based, for the positioning of elements are basically the same, just to add some unique ways more suited to the mobile platform, will focus on the following xpath method , this should be the UI layer elements locate the most powerful methods La!

Taobao app, for example, sweep the top left corner of positioning button

First, the basic positioning

1. If the element text is unique, text by positioning the text

   // * [@ text = 'text attribute text']

# Positioning text 
driver.find_element_by_xpath ( " // * [@ text = 'sweep the'] " ) .click ()

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

   // * [@ resource-id = 'id attribute']

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

 Joint likewise be positioned above two ways, as follows

# May be combined @ resource-id attribute and text attributes @text lower locating 
driver.find_element_by_xpth ( " //*[@resource-id='com.taobao.taobao:id/tv_scan_text'][@text= 'a sweep sweep '] " ) .click ()

3. class attributes only, the same can be positioned by the class attribute, there are two methods

   The first: // class attributes 

# Locate the search box // class attributes 
driver.find_element_by_xpath ( " //android.widget.EditText " ) .click ()

   The second: // * [@ class = 'class attribute'] 

# Location search box // * [@ class = 'class attribute'] 
driver.find_element_by_xpath ( " //*[@class='android.widget.EditText '] " ) .click ()

4. By content-desc attribute Location

   // * [@ content-desc = 'desc text']

# Sign / register 
driver.find_element_by_xpath ( " // * [@ text = 'Register / Login'] " ) .click () 
the time.sleep ( . 3 )
 # Content-Location desc 
driver.find_element_by_xpath ( " // * [@ content-desc = 'help'] " ) .click ()

Two, contains fuzzy positioning

1) contains fuzzy matching positioning method, for id or a text element is not fixed, but some are fixed, which will be fuzzy matching.

  // * [contains (@ content-desc, 'help')]

# The contains match text 
driver.find_element_by_xpath ( ' // * [the contains (@Text, "Register / Login")] ' ) .click () 
the time.sleep ( 3 )
 # the contains match-desc TextContent 
driver.find_element_by_xpath ( " // * [contains (@ content-desc , ' help')] " ) .click ()

2) contains fuzzy matching can id and class attributes

  // * [contains (@ resource-id, 'id attribute')]

  //*[contains(@clsss, ‘class属性’)]

#定位搜索框class
driver.find_element_by_xpath("//*[contains(@class, 'EditText')]").click()
time.sleep(3)
driver.back()
#定位id
driver.find_element_by_xpath("//*[contains(@resource-id, 'id/home_searchedit')]").click()

Third, the combination of positioning

If an element has two attributes, it may be matched by xpath two properties simultaneously, text, resource-id, class, index, content-desc any combination of these properties can be positioned

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

# text index properties and positioning login / register 
desc_class = ' // * [@ text = "Register / Login" and @ = index ". 1"] ' 
driver.find_element_by_xpath (desc_class) .click ( ) 
the time.sleep ( . 3 ) 

# class attributes and text positioning enter the phone number 
class_text = ' //android.widget.EditText[@text= "Please enter a 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()

Fourth, the level positioning

1) If an element, which in addition to the class attribute (the class attribute will certainly be), and consequently there is no other attributes, this case using the above method is not applicable, and this time he can find a parent element, by positioning the father son

# By positioning Father Son search input 
fa_sun = ' //*[@resoure-id="com.taobao.taobao:id/home_searchbar"]/android.widget.EditText ' 
T = driver.find_element_by_xpath (fa_sun) .text
 Print (t)

If a next parent element, a plurality of the same class son, they can pick up a corresponding number of indexing through the xpath, xpath number starting from 1

# 父元素下第2个儿子 微淘
fu_sun2 = '//*[@resource-id="com.taobao.taobao:id/ll_navigation_tab_layout"]/android.widget.FrameLayout[2]'
driver.find_element_by_xpath(fu_sun2).click()

2)相反的,可以通过儿子定位父亲

# 通过子元素定位父元素
# 方法一: ..
sun_fa1 = '//*[@resource-id="com.taobao.taobao:id/tv_scan_text"]/..'
c = driver.find_element_by_xpath(sun_fa1).tag_name
print(c)

# 方法二  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)

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

3)通过子元素,先找到父元素,再找父元素下的子元素,同样可以进行兄弟元素定位

# 兄弟元素
xiongdi = '//*[@resource-id="com.taobao.taobao:id/bar_search"]/../android.widget.RelativeLayout'
x = driver.find_element_by_xpath(xiongdi).tag_name
print(x)

4)通过层级关系,一层一层的往下找,同样可以通过爷爷元素,定位到孙子元素

#爷爷元素FrameLayout---第一个FrameLayout儿子---孙子TextView
x = '//android.widget.FrameLayout/android.widget.LinearLayout[1]/android.widget.TextView'
t = driver.find_elements_by_xpath(x)
print(len(t))
# 打印文本信息
print(t[0].text

 

原文引用: https://www.cnblogs.com/cnkemi/p/9180525.html

Guess you like

Origin www.cnblogs.com/testlearn/p/11487940.html