【问题标题】:Get Absolute XPath of WebElement in Java在 Java 中获取 WebElement 的绝对 XPath
【发布时间】:2020-07-29 13:31:05
【问题描述】:

我知道如何使用以下代码通过Javascript 获取元素的绝对 XPath:

public String getAbsoluteXPath(WebDriver driver)
{
    return  (String) 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]);", this.element);

}

但是,如果我可以访问 HTML source,我不确定如何直接通过 Java 执行此操作。

怎么可能?

Google.com 上徽标的绝对 XPath 为:

html/body/div[1]/div[6]/span/center/div[1]/img

而 Google Logo 的相对 XPath 是:.//*[@id='hplogo']

在给定元素 + HTML 的情况下,如何获取 Web 元素的绝对 XPath?

谢谢

【问题讨论】:

  • 您是否需要仅使用 Java 编程来解决这个问题,或者您是否有能力使用其他一些工具?
  • @AnilChandna 最好使用 Java 本身。

标签: java html xpath


【解决方案1】:

希望以下逻辑有所帮助

function absolutePath(element) {
    if (element.tagName.toLowerCase() == 'html')
        return '/html[1]';
    if (element === document.body)
        return '/html[1]/body[1]';
    var ix = 0;
    var siblings = element.parentNode.childNodes;
    for (var i = 0; i < siblings.length; i++) {
        var sibling = siblings[i];
        if (sibling === element)
            return absolutePath(element.parentNode) + '/' + element.tagName.toLowerCase() + '[' + (ix + 1) + ']';
        if (sibling.nodeType === 1 && sibling.tagName.toLowerCase() === element.tagName.toLowerCase())
            ix++;
    }
}

这是一个更加简化的版本

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 2017-08-17
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多