EN
JavaScript / XPath - get following sibling element
7 points
In this short article we would like to show how to find following sibling element reference using XPath in JavaScript.
In below example we are looking for following slibling (/following-sibling::div
) that occurs as next of any div
element that contains User ID
text inside (//div[text()='User ID']
).
Quic solution:
xxxxxxxxxx
1
2
<html>
3
<body>
4
<div>
5
<div>User ID</div>
6
<div>5f7c2487</div>
7
</div>
8
<script>
9
10
var result = document.evaluate("//div[text()='User ID']/following-sibling::div", document.body);
11
var followingElement = result.iterateNext();
12
13
if (followingElement) {
14
console.log(followingElement.innerText);
15
}
16
17
</script>
18
</body>
19
</html>