简体   繁体   English

要检索给定WebElement的Xpath

[英]Want to Retrieve Xpath of Given WebElement

Using Selenium WebDriver, I am having a list of all web elements in the page. 使用Selenium WebDriver,我可以在页面中找到所有Web元素的列表。 I want to write one function which will return me the XPath string of passed element. 我想编写一个函数,该函数将向我返回所传递元素的XPath字符串。

Call for Function will be like:- String XpathOfElement = myWebDriver.getXpath(My_Web_Element) 函数调用将类似于:- String XpathOfElement = myWebDriver.getXpath(My_Web_Element)

Hint :- I think we can use javascript(using JavaScriptExecuter). 提示 :-我认为我们可以使用javascript(使用JavaScriptExecuter)。 But not familiar with javascript. 但不熟悉javascript。

Check this post , I wrote code to get an absolute XPath. 检查这篇文章 ,我写了代码以获得绝对的XPath。

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);
}

This code works perfectly. 此代码完美地工作。

public String getXpath(WebElement ele) {
    String str = ele.toString();
    String[] listString;
    if(str.contains("xpath")
      listString = str.split("xpath:");
    else if(str.contains("id")
      listString = str.split("id:");
    String last = listString[1].trim();
    return last.substring(0, last.length() - 1);
}

This above function works only if the WebElement has xpath. 仅当WebElement具有xpath时,以上函数才起作用。 Suppose your element has class , then use if-else concept with "class:" as split expression. 假设您的元素具有class ,然后使用if-else概念和“ class:”作为拆分表达式。

Almost any element in DOM can have a lot of valid xPathes. DOM中的几乎任何元素都可以具有很多有效的xPath。 For example input field on Google search page can be found as: //*[@id='lst-ib'] //*[@class='gsfi'][1] //body//div//input[3] ... Which one do you expect to get? 例如,在Google搜索页面上的输入字段可以找到: //*[@id='lst-ib'] //*[@class='gsfi'][1] //body//div//input[3] ...您希望得到哪一个?

Actually google has this algorithm in chrome. 实际上,谷歌在chrome中有此算法。 We can copy xpath of the element and in most cases it is awfull. 我们可以复制元素的xpath,在大多数情况下它是糟糕的。 So it's possible to do, and if it's really vital for you look at NHtmlUnit - you can get dom of the page, find your element there and go up to root element adding tags to path string. 因此这是可能的,如果对NHtmlUnit来说这真的很重要-您可以进入页面的一部分,在其中找到您的元素,然后转到将元素添加到路径字符串的根元素。 I guess, it's poosible to get something like //body/div/div[2]/div[3]/... But why? 我猜想,获得//body/div/div[2]/div[3]/...但是为什么呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM