Selenium2+python Automation 2-element positioning

Source: Internet
Author: User
Tags xpath

Hehe, the book on the back, and then chatter, here first to add the automation to master the four steps: Get the element, manipulate the element, get the return value, the assertion (the return result and expected results are consistent), and finally the automated test report generation. This one focuses on how to position elements. Element positioning in these four links is crucial, if the elements on a page can not be located, then the operation will not continue. Webdriver provides me with 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 and restart the 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 plug-in: XPath checker this can be downloaded, can not download, also can not be downloaded

5. After the plug-in installation is complete, click on the extension of the accessory component, as shown in

Second, view page elements

Take Baidu search box as an example, first open Baidu Web page:

1. Click the Small Reptile button in the top right corner.

2. Click the lower left corner arrow

3. Move the arrows to the Baidu Search input box, enter the box to highlight the status

4. The red area below is the attribute that navigates to the input box:

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

Third, element positioning

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 positioned to this element by its ID attribute.

2. After locating 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.

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 passed through its Class attribute unit to this element.

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 property, 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 example, only for reference and understanding, operation must be an 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.

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. It can be resolved by XPath at this time.

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

3. Follow the steps to copy the corresponding XPath address in the Friepath plugin

Element positioning:find_element_by_css_selector ()

1.css is another syntax, more concise than XPath, performance is better, but not very well understood, I personally also understand is not very thorough (O (╯-╰) o)

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 ()

Not to be continued ...

Selenium2+python Automation 2-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.