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:
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<div>
<div>User ID</div>
<div>5f7c2487</div>
</div>
<script>
var result = document.evaluate("//div[text()='User ID']/following-sibling::div", document.body);
var followingElement = result.iterateNext();
if (followingElement) {
console.log(followingElement.innerText);
}
</script>
</body>
</html>