-
Notifications
You must be signed in to change notification settings - Fork 0
/
getXPath.html
42 lines (36 loc) · 1.05 KB
/
getXPath.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>getXPath</title>
</head>
<body>
<ul>
<li> <span>1</span>
</li>
<li>
<span>2</span> <span>3</span>
<span id="span_3">4</span>
</li>
</ul>
</body>
<script>
function getXPath(node) {
let slibings = node.parentNode.children
let count = 0;
let result = ''
for (let i = 0; i < slibings.length; i++) {
if (slibings[i] === node) {
result = node.tagName.toLowerCase() + (count > 0 ? `[${count}]` : '')
}
if (slibings[i].tagName === node.tagName) {
count++
}
}
return (node.parentNode === document.body ? 'body' : getXPath(node.parentNode)) + '>' + result;
}
console.log(getXPath(document.getElementById('span_3'))) // body>ul>li[1]>span[2]
</script>
</html>