How do you find someone in the real world if you think of the elements on the page as people? There are three methods:
One, through the attributes of the person itself, such as his name, cell phone number, social security number, gender, these can distinguish the attributes of others. The elements on the Web page also have these attributes, such as ID, name, class name, tag name, and so on.
When looking for someone, you can pass the location attribute, for example, X country, X City, X Road, X number. XPath and CSS provide a way to locate a tag-named hierarchical relationship.
Third, can rely on other people's attributes to find someone, for example, I do not have Xiaoming's contact, but I have his father's mobile phone number, then through his father's mobile phone number can also find Xiao Ming. XPath and CSS also provide a way to find the final element through related elements.
Webdriver offers a variety of element positioning methods, which are described in Python language below:
1. ID Location
The HTML rule id attribute must be unique in the HTML document, which is similar to the citizen's ID number. The ID-locating method provided by Webdriver is to find the element through the element's ID attribute. For example, through the ID location browser under the Baidu input box and Baidu Search button, use the following:
find_element_by_id ("kw") Baidu input Box
find_element_by_id ("su") Baidu Search
That is, the find_element_by_id () method locates elements through the id attribute.
2. Name positioning
The HTML rules name to specify the name of the element, just like the person's name, so it is not unique. If you use the name to locate the Baidu input box:
Find_element_by_name ("WD")
The Find_element_by_name () method locates elements through the Name property. Because the Baidu Search button does not provide a name attribute, it cannot be located by using the Name property.
3. Class positioning
The HTML rules CLSS to specify the class name of the element. The usage is similar to the ID, name, such as the Baidu input box and the Search button via the class attribute:
Find_element_by_class_name ("S_ipt")
Find_element_by_class_name ("S_btn")
That is, the Find_element_by_class_name () method locates elements through the class attribute.
4. Tag positioning
The essence of HTML is to use tags to define the implementation of different functions, each element is essentially a tag. Because a tag is often used to define a class of functions, the probability of identifying an element by tag is low. Generally a page has a large number of <div>, <input>, <a> and other tags, it is difficult to mark the tag name to distinguish between different elements. But sometimes can also use, such as through the tag name to locate Baidu input box, Baidu button with the same:
Find_element_by_tag_name ("input")
The Find_element_by_tag_name () method locates elements through the tag name of the element.
5. Link positioning
Link is used to locate text links. such as Baidu in the upper right corner of a few text link code is as follows:
The link locator links are as follows:
Find_element_by_link_text ("News")
Find_element_by_link_text ("hao123")
Find_element_by_link_text ("Map")
Find_element_by_link_text ("video")
Find_element_by_link_text ("Paste")
The Find_element_by_link_text () method locates elements through textual information between the element label pairs.
6. Partial link positioning
Partial link positioning is a supplement to link positioning, some text links will be longer, when we can take part of the text link positioning, as long as this part of the information can uniquely identify the link. As follows:
The partial link is positioned as follows:
Find_element_by_partial_link_text ("A Very long")
Find_element_by_partial_link_text ("Text link")
The Find_element_by_partial_link_text () method locates elements through partial textual information between the element tag pairs.
7. XPath positioning
XPath is a language that locates elements in an XML document. Because HTML can be thought of as an implementation of XML, you can use this powerful language to locate elements in a Web application.
Absolute path Positioning
Such as:
An element always has an absolute path, can be found in the following way Baidu input box and search button:
Find_element_by_xpath ("Html/body/div/div/div/div/div/form/span/input")
Find_element_by_xpath ("Html/body/div/div/div/div/div/form/span[2]/input")
The Find_element_by_xpath () method uses the XPath language to locate elements. XPath primarily uses the hierarchical relationship of the tag name to locate the absolute path of the element, and the outermost layer is the HTML language. In the body text, a first-level search down, if there is more than one level of the same label name, that is in the upper and lower order to determine the number of, for example, Span[2] represents the second span label at the current level.
Positioning with element attributes
In addition to using an absolute path, XPath can also be positioned using the attributes of the element. Also take the Baidu input box and search button as an example:
Find_element_by_xpath ("//input[@id = ' kw ']")
Find_element_by_xpath ("//input[@id = ' su ']")
Indicates that the current page is under a directory, input represents the label name of the positioned element, [@id = ' kw '] indicates that the element's id attribute value equals kw. The following are positioned by the name and class property values:
Find_element_by_xpath ("//input[@name = ' wd ']")
Find_element_by_xpath ("//input[@class = ' s_ipt ']")
Find_element_by_xpath ("//*[@class = ' s_btn ']")
If you do not want to specify a label name, you can use an asterisk (*) instead. Of course, using XPath is not limited to ID, name, class three attribute values, any attribute value of an element can be used, as long as it uniquely identifies an element.
Find_element_by_xpath ("//input[@maxlength = ' 100 ']")
Find_element_by_xpath ("//input[@value = ']")
Find_element_by_xpath ("//input[@type = ' snbmit ']")
Combining hierarchies with attributes
If an element itself does not have an attribute value that uniquely identifies the element, then it can be found on its previous level, if its previous level has a value that uniquely identifies the property, or it can be used.
If the Baidu input box itself has no available attribute value, then you can find its upper-level properties, as follows:
Find_element_by_xpath ("//span[@class = ' s_ipt_wr ']/input")
span[@class = ' bg s_ipt_wr ') locates the parent element through the class attribute, followed by/input to represent the child element below the parent element. If the parent element has no available property values, you can continue to look up the "Grandpa" element.
Find_element_by_xpath ("//form[@id = ' form ']/span/input")
Find_element_by_xpath ("//form[@id = ' form ']/span[2]/input") #百度搜索按钮
This method can be looked up at the first level, up to the outermost
Using logical operators
If a property cannot have one element in a unique region, we can also use a logical operator to concatenate multiple properties to find an element. If you use both the ID and class to uniquely identify the Baidu input box, connect two conditions through the logical operator "and". As follows:
Find_element_by_xpath ("//input[@id = ' kw ' and @class = ' s_ipt ']/span/input")
Of course, you can use "and" to concatenate more properties to uniquely identify an element.
8. CSS Positioning
CSS (cascading Style Sheets) is a language that is used to describe the performance of HTML and XML documents. CSS uses selectors to bind properties to page elements. CSS can be more flexible to select any property of the control, in general, positioning faster than XPath, but it is also more difficult. The following is a brief introduction to the syntax and use of CSS.
The syntax of the CSS selector is shown in the following table:
| Selector Selector |
Example |
Describe |
| . class |
. Inrto |
Class selector, select all elements of class= "Intro" |
| #id |
#firstname |
ID selector, select all elements of id= "FirstName" |
| * |
* |
Select all over elements |
| Element |
P |
Element all <p> elements |
| Element>element |
Div>input |
Selects all <input> elements after all elements of the parent element are <div> |
| Element+element |
Div+input |
Select all <input> elements in the same level immediately after the <div> element |
| [Attribute=value] |
[Target=_blank] |
Select all elements of target= "_black" |
Also take the Baidu input box and the search button as an example to introduce the use of CSS positioning
1) Navigate by class attribute:
Find_element_by_css_selector (". S_ipt")
Find_elemetn_by_css_selector (". S_btn")
The Find_element_by_css_selector () method is used to locate elements in the CSS language, and the dot (.) indicates that the element is positioned through the class attribute.
2) location by id attribute:
Find_element_by_css_selector ("#kw")
Find_element_by_css_selector ("#su")
The pound sign (#) indicates that the element is positioned by the id attribute.
3) positioning by tag name
Find_element_by_css_selector ("input")
In the CSS language, the tag name is used to locate the element without any symbolic identification, and the tag name is the right one. There are several ways to navigate:
(1) Locating by parent-child relationship:
Find_element_by_css_selector ("Span>input")
Indicates that there is a FATHER element whose label is named Span and finds all its tags called the child elements of input.
(2) by attribute positioning:
Find_element_by_css_selector ("[value=]")
Find_element_by_css_selector ("[name= ' wd ']")
Find_element_by_css_selector ("[type= ' Submit ']")
Any attribute of an element can also be used in CSS, as long as these attributes uniquely identify the element. The value of the attribute can be quoted or not added, but the attention is distinguished from the quotation marks of the entire string.
(3) Combination positioning:
The above positioning strategy can be combined to use, can greatly enhance the uniqueness of the positioning element.
Find_element_by_css_selector ("Span.s_ipt_wr>input.s_ipt")
Find_element_by_css_selector ("Span.s_btn_wr>input#su")
There is a parent element whose label is called span; it has a class attribute value called S_IPT_WR; it has a child element, the tag is called input, and the property value of the class of the child element is called S_ipt. What you're looking for is a child element with so many features.
9. Use by to locate the element
Webdriver also provides an additional set of find_element () methods that can be called uniformly, declaring the positioning method by using by, and passing in the positional parameters of the corresponding positioning method. Specific as follows:
Find_element (by.id, "kw")
Find_element (By.name, "WD")
Find_element (By.class_name, "S_ipt")
Find_element (by.tag_name, "input")
Find_element (By.link_text, "News")
Find_element (By.partial_link_text, "new")
Find_element (By.xpath, "//*[@class = ' s_btn ']")
Find_element (By.css_selector, "Span.s_btn_wr>input#su")
The Find_element () method is used only for locating elements. It requires two parameters, the first parameter is the type of the anchor, is provided by the by, and the second parameter is the exact way to locate it.
The By class import is required before using by.
From selenium.webdriver.common.by Import by
General Webdriver More recommend the wording described earlier.
For Web automation, the learning element is positioned equal to half of what has been learned, and the rest is the various methods available in Webdriver. So practice the element positioning method, laying the groundwork for the back.
Webdriver Positioning Element Method