Python Selenium series (ii) Element positioning method

Source: Internet
Author: User
Tags tag name xpath

a preface

element positioning, which is the first step in manipulating elements, is also WebUI the difficulty and core of automation.

two element positioning method

Selenium provides a built-in approach to the positioning of operation elements, divided into 8 categories, each of which can be subdivided into locating individual elements and locating multiple elements, plus 2 private methods. Details are as follows:

Locating a single element

· find_element_by_id

· find_element_by_name

· find_element_by_xpath

· find_element_by_link_text

· find_element_by_partial_link_text

· find_element_by_tag_name

· find_element_by_class_name

· find_element_by_css_selector

Positioning multiple elements (note elements , return a list)

· find_elements_by_id

· find_elements_by_name

· find_elements_by_xpath

· find_elements_by_link_text

· find_elements_by_partial_link_text

· find_elements_by_tag_name

· find_elements_by_class_name

· find_elements_by_css_selector

Two private methods (derived from basic method)

find_element   and   find_elements  

The two private methods essentially correspond to the single-element positioning and multi-element positioning described above. 8 class method, as follows:

From selenium.webdriver.common.by Import by

Driver.find_element (By.xpath, '//button[text () = "Some text"])

Driver.find_elements (By.xpath, '//button ')

by Properties available for the class

ID = "id"

XPath = "XPath"

Link_text = "link TEXT"

Partial_link_text = "partial LINK TEXT"

Name = "name"

Tag_name = "tag NAME"

Class_name = "Class NAME"

Css_selector = "css SELECTOR"

three positioning Property Description

Take the Baidu page as an example, explain the location of the properties in the page source code representation, as follows:

Open Baidu page, press keyboard F12, open the development tool, use the arrow in the upper left corner, click on the page element, you can switch to the source part of the element.

by_id: "KW"

By_name:"WD"

By_class_name:"S_ipt"

By_tag_name: "Input"

By_link_text: " News "

By_partial_link_text: " new "

Xpath and CSS can be obtained directly using the development tools, as follows:

selected element -à right -àcopy-àcopy xpath/copy selector

By_xpath: "//*[@id =" kw "]"

By_css_selector: "#kw"

four summary

just so. WebUI element Positioning is the core, because the element must be positioned before manipulating the element, so the element positioning is the difficulty, because selenium provides the method of locating elements, but some scenarios directly apply the method will be error, such as the element is embedded into the Frame, there are many identical elements, bullets, loading delay, etc., at this time need to analyze the elements, and then take the response, which is also the experience of WebUI automation is accumulated. The next article will be devoted to two elements of positioning ideas.

Other resources:
about the python Selenium for a video explanation of the element positioning method, see: http://i.youku.com/weiworld521 Section - section.


Python Selenium series (ii) Element positioning method

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.