Selenium+python element Positioning

Source: Internet
Author: User
Tags xpath

Automation takes four steps: Get elements, manipulate elements, get return results, assert (return results are consistent with expected results), and finally automatically test reports.

Element positioning is critical in these four links, where the element positioning is 70%, if it is allocated by the learning effort, the action element 10%, the return result 10%, and the assertion 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" >



Three, element positioning: 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


Element positioning: 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


Element positioning: 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 positioned to this element by its Class property.


Element positioning: 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


Element positioning: Find_element_by_link_text ()

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


Element positioning: Find_element_by_partial_link_text ()

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

Element positioning: 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


Element positioning: Find_element_by_css_selector ()

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

Summarize:

Selenium's Webdriver provides eight basic element positioning methods, the first six are directly positioned through the attributes of the elements, and later XPath and CSS positioning 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_text ()

6. Positioning via Partial_link: Find_element_by_partial_link_text ()

7. Positioning via XPath: Find_element_by_xpath ()

8. Positioning via CSS: Find_element_by_css_selector ()

Selenium+python element Positioning

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.