XPATH positioning based on python+selenium

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

There are many methods for selenium to locate elements, such as through id, name, class_name, tag_name, link_text, etc., but these methods are too limited. Taking the id attribute as an example, first of all, every element must not have an id attribute. Secondly, the id attribute of the element is not necessarily fixed. Therefore, it is enough to understand these methods. What we really need to master is positioning through xpath and css. Generally, we only need to master one of them to deal with most of the positioning work.

 

The following summarizes the positioning method of xpath, and deepens the understanding with the examples of your own practice.

First, you need to understand the concept and basic syntax of xpath

There are relatively few tutorials about xpath on the Internet. You can take a look at the rookie tutorial and related materials of w3cschool: http://www.runoob.com/xpath/xpath-tutorial.html , http://www.w3school.com.cn/xpath /index.asp

 

 

Second, the commonly used xpath positioning method

1. Use the attributes in the tag for positioning

(1) Through the id attribute

xpath = "//a[@id='start_handle']" 
//a means to select all a elements, plus [@id='start_handle'] means to select a element whose id attribute is 'start_handle'
(2) Locate by name attribute

 

xpath = "//input[@name='custName']"

 

Summary : xpath = "//tag name[@attribute= 'attribute value' ]"

Attribute judgment conditions: the most common are id, name, class, etc. There are no special restrictions on the category of attributes, as long as an element can be uniquely identified.

When an attribute is not enough to uniquely distinguish an element, a combination of multiple conditions can also be adopted, as follows:

xpath= "//input[@type='XX' and @name='XX']"

 

2. Use the text() method to locate

As shown above, there is a piece of text "Previous" between the <a></a> tags of the [Previous] button, then it can be positioned in the following way

xpath = "//a[text()='previous step']"

The [Start processing] button can use the following path : xpath = "//a[text()='Start processing']"

 

3. Use the contains() method to locate, also called fuzzy positioning

xpath = "//tag name [contains(@attribute, 'attribute value')]"

 

xpath3 = "//a[contains(@href, 'basicinfo')]" 
Take the key information
'basicinfo' of href, so that you can also locate the [Start Processing] button


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325339212&siteId=291194637
Recommended