XPath positioning of the Python+selenium base

Source: Internet
Author: User
Tags tag name xpath

The furthest distance in the world is probably to see a page element chu there, but I can't locate it!!

There are many ways to locate elements, such as through IDs, name, Class_name, Tag_name, Link_text, and so on, but the limitations of these methods are selenium, and the id attribute, first of all, must not have an id attribute for each element. The id attribute of the second element is not necessarily invariant. So these methods to understand, what we really need to master is through the XPath and CSS positioning, generally as long as the master one can handle most of the positioning work.

Here is a summary of the XPath positioning method, combined with the practice of their own examples to deepen the understanding

First, we need to understand the concept of XPath and basic syntax

Online about the XPath tutorial less, you can look at the Novice tutorial and W3cschool related information:http://www.runoob.com/xpath/xpath-tutorial.html,/http Www.w3school.com.cn/xpath/index.asp

Second, the common XPath localization method

1. Use the attributes within the tag to locate

(1) by id attribute

XPath = "//a[@id = ' start_handle ']"
A indicates that all a elements are selected, plus [@id = ' start_handle '] to select the A element with the id attribute ' Start_handle '
(2) Locating by Name property


XPath = "//input[@name = ' CustName ']"

Summarize : XPath = "//tag name [@ attribute =' property value ']"

Attribute criteria: Most commonly id,name,class, and so on, the category of attributes has no special restrictions, as long as one element can be uniquely identified.

When an attribute is not sufficient to uniquely differentiate an element, you can also take multiple combinations of conditions, as follows:

Xpath= "//input[@type = ' xx ' and @name = ' xx ']"

2. Use the text () method to locate

As shown above, there is a text "back" between the <a></a> tags of the "previous" button, so you can locate them in the following way

XPath = "//a[text () = ' Back ']"

the "Get Started" button can use the following path :xpath = "//a[text () = ' start processing ']"

3. Using the contains () method to locate, also known as fuzzy positioning

XPath = "//Tag name [contains (@ attribute, ' property value ')]"


Take the key information of the href ' basicinfo ', so you can also navigate to the "Get Started" button


Python+selenium Base XPath positioning

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.