Python3 Selenium automated web testing ==> Section X WebDriver advanced applications - xpath syntax

Learning objectives:


 

  xpath position is for conventional positioning methods, the most effective way of positioning.


Scenes:


 

  The positioning of page elements.  

 

Official steps:


 

step1: General Properties

Examples of UI

 

Related HTML code example of a UI

The relevant code examples:

# The id positioning 
dr.find_element_by_xpath ( ' // * [@ id = "username-LoginForm"] ' ) .click ()
 # by tag label location 
# matches any tag asterisk 
dr.find_element_by_xpath ( ' // * [@ = ID "LoginForm-username"] ' ) .click ()
 # label name is specified 
dr.find_element_by_xpath ( ' // INPUT [@ = ID "LoginForm-username"] ' ) .click ()
 # by targeting class 
dr.find_element_by_xpath ( ' // * [@ class = "form-Control"] ' ) .click ()
 # by name anchor 
dr.find_element_by_xpath ( ' // * [@ name = "LoginForm[username]"]').click()

  

 step2: Other attributes


 


#
Among other properties, is herein Africa id, name, class attributes of some pages located dr.find_element_by_xpath ( ' // * [text () = "text page positioning"] ' ) # plurality of attribute combinations dr. find_element_by_xpath ( ' // * [@ type = "text" and @ ID = "username-LoginForm"] ' )

  

step3: hierarchy


 

a. If an element, its attributes is not very clear, can not be located directly, this time we can find someone that father (parent element)
b. After the father to find it, find the next level will be able to navigate to the

HTML code Interpretation

solution

# By father node location 
dr.find_element_by_xpath ( ' // * [@ class = "form-Group-Field-LoginForm-error has required username"] / INPUT ' ) .click ()
 # by grandfather node location 
dr.find_element_by_xpath ( ' * // [@ ID = "Login-form"] / div [. 1] / div / INPUT ' )

 

 

step4: Positioning Index


 

Html code demonstrates

solution:

# XPath positioning index, an index value from the initial start 
dr.find_element_by_xpath ( ' // SELECT [@ ID = "NR"] / Option [1] ' ) .click ()

 

 

 

step5: fuzzy matching


 

1.xpath there is a very powerful feature, fuzzy matching
2. master fuzzy matching, basically not positioned less than
3. For example, I want to locate a hyperlink Baidu page "hao123", speaking in the last article over by by_link, can also by_partial_link, fuzzy matching target. Of course xpath may have the same function, and more powerful.
# Positioning Baidu home page hao123 
dr.find_element_by_xpath ( ' // * [the contains (text (), "hao123")] ' ) .click ()
 # fuzzy matching a property for Baidu search box 
dr.find_element_by_xpath ( ' // * [the contains (@id, "kw") and @class = "s_ipt"] ' ) .click ()
 # fuzzy matching begins with what 
dr.find_element_by_xpath ( ' // * [Soho starts-with (@class, "s_ip") ] ' ) .click ()

 

Difficulties:


 

  There is no difficulty, is to multi-purpose, learn and use.

 

summarize:


 

   xpath control 80% of the pages targeting solution

Guess you like

Origin www.cnblogs.com/wuzhiming/p/11324872.html