About Xpath

1.xPath Profile

XPath is a find information in XML and HTML documents in languages ​​that can be used to traverse the elements and attributes in XML and HTML documents

XPath installation

Chrome plug-in XPath Helper

Upper right corner of the browser spot Chrome: extensions more tools ----- ----- Google store -------- Check XPath Helper (need over the wall)

2. Detailed grammar

Xpath node type:

Element, attribute, text, namespace, processing instructions, comments and documents

Xpath: the path expressions to select nodes or node location of the XML document

 

 

 

Methods: First, find the directory element is the "precise elements" property that is the only way to identify, locate, locate this attribute is used;

1. By positioning the element itself unique properties

   Methods: where to find the target element "precise elements" that uniquely identifies the property, use this property to target

  • By id attribute positioning 1.1
  • Example: find_element_by_xpath ( " // INPUT [@ ID = 'INPUT'] " )       # @ followed by the property may be any property
  • 1.2 positioned by the name attribute
  •      例:find_element_by_xpath("//div[@name='q']")

    2. With a unique directory attribute positioning

    Methods: The target is not the only element attribute, to find the target element similar to the parent directory "unique element" as a starting position, and based on this relative position of the layer by layer to write to the target location subdirectory

Example:

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

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

find_element_by_xpath("//div[@name='q']/form/span/input")

3. xpath do Boolean

 

find_element_by_xpath ( " // div [@ id = 'hd' or @ name = 'q'] " ) # Find id or name is q is the hd

 

4. The dual filter conditions are

 

find_element_by_xpath("//div[@id='hd'][@name='q'")

 

5. The existence of hierarchical relationships catalog elements

Example 1:

 find_element_by_xpath("//ul[@class='app-list']/li[contains(@class,'safe')]/div")

 

Example 2: re-positioning of one element targets (repositioning positioning dl dt)

 

find_element_by_xpath("//form[@id='J_login_form]/dl/dt/input[@id='J_password']") 

 

6. fuzzy positioning

6.1 contains methods (included)

find_element_by_xpath ( " // A [the contains (@ name, 'trnews')] " ) # method for obtaining the element is trnews

 

6.2 start-with method (beginning with XX)

find_element_by_xpath ( " // A [Start-with (@ the href, 'http')] " ) # Select element beginning with http

 

6.3 text method

find_element_by_xpath ( " // A [the contains (text (), 'news')] " ) to find the hyperlink text content elements

 

find_element_by_xpath ( " // * [text () = 'news'] " ) Find all content elements of the word quit

 

7, xpath contains some logic Usage

// * [count (XXX) = 2] // count the number of elements = XXX node 2
 // * [local-name () = ' xxx ' ] // find the tag element is xxx
 // * [starts- with (local-name (), ' x ' )] // find all the elements at the beginning of the tag to x
 // * [the contains (local-name (), ' x ' )] // find all tag contained element x
 / / * [string-length (local -name ()) = 3] // find all the tag element length is 3
 // xxx | // yyy // plurality Pathfinder

Guess you like

Origin www.cnblogs.com/qq991025/p/12076590.html