First, XPath basic positioning usage
1.1 Using ID to locate--driver.find_element_by_xpath ('//input[@id = "kw"])
1.2 Using class positioning-driver.find_element_by_xpath ('//input[@class = "S_ipt"]
1.3 Of course, through the usual 8 ways of combining XPath can be located (name, Tag_name, Link_text, Partial_link_text) above only listed 2 common ways OH.
Second, XPath relative path/absolute path location
2.1 Relative positioning-Starting with//://form//input[@name = "Phone"]
2.2 Absolute positioning-with/start, but to start from the root directory, more cumbersome, generally not recommended to use such as:/html/body/div/a
XPath text, fuzzy, logical positioning
3.1 "Text positioning" uses the text content of the text () element such as://button[text () = "Login"]
3.2 "Fuzzy positioning" using contains () contains functions such as://button[contains (Text (), "login"),//button[contains (@class, "btn")]
3.3 "Fuzzy positioning" uses starts-with-match attribute values starting with XX, ends-with-matches attribute values ending with xx such as://button[starts-with (@class, "btn"),//input[ Ends-with (@class, "-special")]
3.4 Using logical operators--and, or, for example://input[@name = "Phone" and @datatype = "M"]
Four, XPath axis positioning
4.1-axis operation
Ancestor: Ancestor nodes include parent
Parent: Father Node
Prceding-sibling: All sibling nodes before the current element node label
Prceding: All nodes before the current element node label following-sibling: All sibling nodes after the current element node label following: All nodes after the current element node label use syntax: Axis name:: node name uses more scenes: page display Data columns shown as a table style are as follows:
Attention:
#定位 find the element--to be uniquely identified
#优先使用id
#舍弃: The presence of subscripts, the presence of absolute positioning, and the abandonment of dynamic ID changes
Python-xpath Positioning Method Detailed