WebDriver API 1 --- XPath (Mushishi "selenium3 automated testing combat - based on the Python language note 9")

XPath Location: find_element_by_xpath () method

1. Absolute positioning path

For example: the absolute path Baidu input box positioning:

# Baidu input box 
find_element_by_xpath ( " / HTML / body / div / div / div / div / div / form / span / the INPUT " )
 # Baidu search button 
find_element_by_xpath ( " / HTML / body / div / div / div / div / div / form / span [2] / INPUT " ) 
Note: span [2] means the second span in the current levels

2. Use positioning element attributes

find_element_by_xpath ( "// tag name or * [@ attribute = attribute value]")

find_element_by_xpath("//input[@id = 'kw']")
find_element_by_xpath("//*[@id = 'kw']")

Xpath is not limited id, name, class attribute value, as long as a unique identification element

3. The level combined with the property

 find_element_by_xpath ( "// tag name or 1 * [@ attribute name =" attribute value '] / tag name 2 ")

If the element does not have a property value that uniquely identifies the element, the element can be on a look through.

E.g:

 

 By positioning the upper Baidu input box:

find_element_by_xpath("//span[@class='bg s_ipt_wr']/input")

Baidu positioned on the upper input box by:

find_element_by_xpath("//form[@id='form']/span/input")

4. Using Logical Operators

And connecting a plurality of logical operators to find the element properties, showing a plurality of conditions must be met positioning element

5. The method contains 

For a string matching attribute contained

For example: class attribute span tag is "bg s_ipt_wr" (the identification number of the position in FIG. 10)

find_element_by_xpath("//span[contains(@class,'s_ipt_wr')]/input")

6. Use text () method

Matching the text information for display

E.g:

find_element_by_xpath ( " // A [text (), 'news'] " )
 # is equivalent to link text positioning text 
find_element_link_text ( " news " )

and contains text () in combination with:

find_element_by_xpath ( " // A [the contains (text (), 'a long')] " )
 # is equivalent to the partial link anchor texts 
find_element_by_partial_link_text ( " a very long " )

 

Guess you like

Origin www.cnblogs.com/kite123/p/11452365.html