Last active
December 17, 2017 19:57
-
-
Save 4M01/8e80a428adba7893314a9ed8a5f655fc to your computer and use it in GitHub Desktop.
This method accepts webelement as parameter and returns absolute XPath string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String getAbsoluteXPath(WebElement element){ | |
return (String) ((JavascriptExecutor) driver).executeScript( | |
"function absoluteXPath(element) {"+ | |
"var comp, comps = [];"+ | |
"var parent = null;"+ | |
"var xpath = '';"+ | |
"var getPos = function(element) {"+ | |
"var position = 1, curNode;"+ | |
"if (element.nodeType == Node.ATTRIBUTE_NODE) {"+ | |
"return null;"+ | |
"}"+ | |
"for (curNode = element.previousSibling; curNode; curNode = curNode.previousSibling) {"+ | |
"if (curNode.nodeName == element.nodeName) {"+ | |
"++position;"+ | |
"}"+ | |
"}"+ | |
"return position;"+ | |
"};"+ | |
"if (element instanceof Document) {"+ | |
"return '/';"+ | |
"}"+ | |
"for (; element && !(element instanceof Document); element = element.nodeType == Node.ATTRIBUTE_NODE ? element.ownerElement : element.parentNode) {"+ | |
"comp = comps[comps.length] = {};"+ | |
"switch (element.nodeType) {"+ | |
"case Node.TEXT_NODE:"+ | |
"comp.name = 'text()';"+ | |
"break;"+ | |
"case Node.ATTRIBUTE_NODE:"+ | |
"comp.name = '@' + element.nodeName;"+ | |
"break;"+ | |
"case Node.PROCESSING_INSTRUCTION_NODE:"+ | |
"comp.name = 'processing-instruction()';"+ | |
"break;"+ | |
"case Node.COMMENT_NODE:"+ | |
"comp.name = 'comment()';"+ | |
"break;"+ | |
"case Node.ELEMENT_NODE:"+ | |
"comp.name = element.nodeName;"+ | |
"break;"+ | |
"}"+ | |
"comp.position = getPos(element);"+ | |
"}"+ | |
"for (var i = comps.length - 1; i >= 0; i--) {"+ | |
"comp = comps[i];"+ | |
"xpath += '/' + comp.name.toLowerCase();"+ | |
"if (comp.position !== null) {"+ | |
"xpath += '[' + comp.position + ']';"+ | |
"}"+ | |
"}"+ | |
"return xpath;"+ | |
"} return absoluteXPath(arguments[0]);", element); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment