Element positioning of Selenium+python automation

Source: Internet
Author: User
Tags xpath

Automated step splitting can be divided into four steps: locating elements, manipulating elements, getting return results, asserting (returning results consistent with expected results), and finally automatically testing the report. This article follows the basic eight element positioning methods. The popular point is to teach everyone to find the object.

All things are objects, so-called Object , is the goddess you like, she is your object. Since it is the object, she has properties , such as large eyes, long hair, skin white, Yan value high. These are her attributes, and each attribute has a corresponding property value , such as eye ="Big", hair ="Long", yan value = "High", corresponding to the = This is followed by the value of its property.

By the same token, the element on a page is treated as an object (your goddess), and we can find her by her attribute value.

element Positioning in these four links is crucial, if said by the study of energy distribution, the element positioning accounted for 70%, theoperation element 10%, get the return result 10%; assert 10%. If an element on a page cannot be located, the subsequent operation cannot continue. Next, webdriver provides eight basic element positioning methods.

First, the Environment preparation:

1. Browser selection:Firefox

2. Install plugins:Firebug and firepath ( settings Add-ons search: Enter plug-in name) download restart browser after installation )

3. after the installation is complete, there is a small reptile icon in the upper right corner of the page

4. Quick View XPath plugin:XPath Checker This can be downloaded or not downloaded

5. after the plug-in installation is complete, click on the add-ons extension, as shown in

Second, view the page elements:

Baidu search box For example, first open Baidu Web page

1. Click the crawler button on the top right corner

2. Click the lower left corner arrow

3. speak the arrows to move to the Baidu Search input box, the input box highlighting state

4. The red area below is the property of the unit to the input box:

<input id= "kw" class= "S_ipt" type= "text" autocomplete= "Off" maxlength= "Up" name= "WD" >

third, element location ID:find_element_by_id ()

1. from the element attribute anchored above, you can see that there is an ID attribute:id= "Search-key", which can be passed through its ID attribute unit to this element.

2. after locating to the search box, use the Send_keys () method

iv. positioning of elements name : Find_element_by_name ()

1. from the element attribute that is anchored above, you can see that there is a name property:name= "WD", which can be passed to this element by its name attribute unit.

Note: This operation will result in an error, indicating that the Name property of the search box is not unique and cannot be directly anchored to the input box via the Name property

Five, element positioning class : Find_element_by_class_name ()

1. from the element attribute anchored above, you can see that there is a class attribute:class= "S_ipt", which can be passed through its class attribute unit to this element.

Six, element positioning Tag : Find_element_by_tag_name ()

1. from the element attributes that are anchored above, you can see that each element has a tag attribute,such as the label property of the search box, which is the first input

2. obviously, in a page, the same label has a lot of, so generally do not use tags to locate. The following examples, only for reference and understanding, run a positive error

vii. positioning of elements Link : Find_element_by_link_name ()

1. Locate the "hao123" button on the Baidu page

To view page elements:

<a class= "Mnav" target= "_blank" href= "http://www.hao123.com" >hao123</a>

2. from the element attribute can be analyzed, there is an href = "http://www.hao123.com

Indicates that it is a hyperlink, for this element, you can use the following methods

Eight, element positioning Partial_link : Find_element_by_partial_link_name ()

1. sometimes a hyperlink to its string may be longer, if you enter the full name, it will be very long, this time you can use a fuzzy matching method, you can intercept a portion of the string.

2. such as "hao123", just enter "ao123" can also be located to

Nine, element positioning XPath : Find_element_by_xpath ()

1. The above positioning method is positioned by a property of the element, if an element does not have an ID,name,class attribute, or hyperlink, do it? Or it's a lot of properties that are duplicated. At this point, you can use XPath to solve

2.xpath is a path language, with the above positioning principle is not the same, first first step to learn to use the tool to view the XPath of an element

3. installation steps, copy the corresponding XPath address in the friepath plugin

10. Element positioning: Find_element_by_css_name ()

1.css is another syntax that is more concise than XPath, but not very well understood. Here first learn how to use tools to view, follow-up tutorials and further in-depth explanation

2. Open the Firepath plugin to select CSS

3. position to the rear as shown in red area

Final Summary:

Selenium 's webdriver provides eight basic element positioning methods, the first six are directly positioned through the attributes of the elements, and the subsequent XPath and CSS positioning is more flexible, Need to focus on one of them.

1. Locating by ID :find_element_by_id ()

2. Positioning by name :find_element_by_name ()

3. through class positioning:find_element_by_class_name ()

4. tag positioning:find_element_by_tag_name ()

5. Positioning via link :find_element_by_link_name ()

6. Positioning via partial_link :find_element_by_partial_link_name ()

7. Positioning via XPath :Find_element_by_xpath ()

8. Positioning via css :find_element_by_css_name ()

Element positioning of Selenium+python automation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.