Common positioning methods of web automation
Why learn positioning
1. Let the program operate on the specified element, it must first find this element
2. The program is not like a human being who directly locates the element with the eyes
-
webDriver provides eight ways to locate elements
Summary of positioning methods
1.id, name, class_name, tag_name: locate according to the label of the element or the attribute of the element
2.link_text, partial_link_text: locate according to the text of the hyperlink (a label)
3.xpath: Positioning for the element path -- focus 4.cSs: Positioning for the css selector (style positioning)
ID positioning
Explanation: HTML stipulates that the id attribute must be unique in the entire HTML document, and id positioning is to locate the element through the id attribute of the element ;
Prerequisite: The element has an id attribute
id location method: find_element_by_id0
Requirements: Open the Baidu interface ( https://www.baidu.com/ ), locate by id, enter information, and click the Baidu button
tag _name label positioning
Note: Since there are often many identical tag names in the HTML source code, this positioning method is generally not used
tag _name is located by the tag name, such as a tag
link textpositioning
Explanation: The link_text positioning is different from the previous 4 positionings, it is specially used to locate the hyperlink text (<a>text value</a>)
Premise: The positioned element is a link tag (a tag)
link_text positioning method: find_element_by_link_text()
Open the Baidu homepage, locate the [News] button through link_text (link text), and click on it
Element Group Positioning
Element group location method: find_elements_by_xxx
effect:
1. Find and return to locate all eligible elements
2. The format of the returned positioning element is list format
illustrate:
The reading of the list data format needs to specify the subscript (the subscript starts from 0)
Case requirements: open the Baidu page
https://www.baidu.com/, locate by element group > locate: "//*[@id='s-top-left']/a"
xpath location
Overview of xpath:--location positioning (path mode)
1.xpath is the abbreviation of xml path, which is a language used to determine the location of a certain part in an XML document.
2. HTML can be regarded as an implementation of XML, so selenium users can use this powerful language to locate elements in web applications 3. xpath is a powerful language because it has a very flexible positioning strategy.
Positioning method: find_element_by_xpath()
xpath positioning strategy (mode)
1. Path positioning--absolute path, relative path
2. Use element attributes to locate
3. Combining positioning with levels and attributes
4. Combination of attributes and logical positioning
Path positioning
Absolute path : all paths from the outermost element to the specified element through the element hierarchy; such as /html/body/div/p[2]
Tips :
1. The relative path starts with //
2. View element properties through the browser, right-click to copy xpath to quickly generate
xpath expression description and format
xpath locates by attribute
xpath locates through the existing attributes of the element, such as id, name, etc.
xpath locates elements through text text
The current element does not have the attributes of id.name, how to locate it?
Open the mall interface (http:llshopxo.hctestedu.com/index.php?s=/indexluser/logininfo.html), through
xpath location post
Box Login Tab
Definition element: ll*[text()="email verification code"]
xpath locates elements by hierarchy
The element to be found has no attributes, but its parent does;
url = "http:llshopxo.hctestedu.com/index.php?s=/index/userllogininfo.html"
Example: lI*[@class='login-top']/a
xpath logical operation
Solve the problem of duplicate names of the same attribute between elements;
Example: 'll*[text()="register" and @class="am-btn am-btn-secondary am-btn-xs am-radius"]'
http:/llshopxo.hctestedu.com/index.php?s=/index/userlogininfo.html