Since the latest version of Firefox no longer supports development tools such as FireBug, you can add extensions such as Firebug by downloading Firefox versions below 49 from https://ftp.mozilla.org/pub/firefox/releases/ .
What is XPath?
XPath is the path language of XML. In layman's terms, it is to find the tag element through the path of the element.
tool
Xpath practice suggests that after installing the Firefox browser, download the plug-in, FireBug.
How to use Xpath
Note: The default dead format is written first //* represents all elements under the positioning page
1. Xpath supports ID, Class, Name positioning functions
1
2
3
4
5
6
7
8
9
10
11
|
通过
ID定位
//*[@id='kw']
通过
Class定位
//*[@class='class_name']
通过
Name定位
//*[@name='name']
|
2. If the tag does not have the three total attributes of ID, Class and Name, Xpath also supports the attribute positioning function
1
2
3
|
@ 代表以属性定位,后面可以接标签中任意属性
//*[@other='attribute']
|
3. When the attributes of tags are repeated, Xpath provides filtering by tags
1
2
3
|
将
* 换位任意标签名,则可根据标签进行筛选
//input[@placeholder='用户名']
|
4. Xpath provides hierarchical filtering when tabs are repeated
For example: if you can't find your son, then you will find his father first, if you can't, you can look for his grandfather
1
2
3
4
5
6
7
|
支持通过
/ 进行层级递进,找到符合层级关系的标签
//form/div/input[@placeholder="用户名"]
当层级都重复时,可以通过单个层级的属性进行定位
//form/div[@class='login-user']/input
|
5. An element's sibling elements are the same as its label, and cannot be located through the hierarchy at this time. Because they are all born to one father, they are twin brothers. Xpath provides index filtering
1
2
3
|
通过索引,在
List中定位属性,与python的索引有些差别,Xpath从1开始
//select[@name='city'][1]/option[1]
|
6. If the above concentration is repeated after use, we can use the ultimate artifact provided by Xpath, logical operation positioning. and or
1
2
3
4
5
6
7
|
通过
and来缩小过滤的范围,只有条件都符合时才能定位到
//select[@name='city' and @size='4' and @multiple="multiple"]
or就相反了,只要这些筛选中,其中一个出现那么久匹配到了
//select[@name='city' or @size='4']
|
I usually think of Xpath as a dragon slayer. Martial arts supreme, treasure knife Xpath, Css is out, who will compete with?