首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Geckofx从geckoelement获取xpath

Geckofx从geckoelement获取xpath
EN

Stack Overflow用户
提问于 2014-08-18 06:32:15
回答 1查看 1.4K关注 0票数 1

如何使用geckofx从geckoelement获得xpath?

代码语言:javascript
复制
private void webBrowser_DomClick(object sender, DomEventArgs e)
{
    GeckoElement clickedElement = e.Target.CastToGeckoElement();                      
}

clickedElement xPath怎么来的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-21 05:04:53

代码语言:javascript
复制
    public static string GetSmallXpath(this GeckoNode node)
    {
        if (node.NodeType == NodeType.Attribute)
        {

            return String.Format("{0}/@{1}", GetSmallXpath(((GeckoAttribute)node).OwnerDocument), node.LocalName);
        }
        if (node.ParentNode == null)
        {
            return "";
        }
        string elementId = ((GeckoHtmlElement) node).Id;
        if (!String.IsNullOrEmpty(elementId))
        {
            return String.Format("//*[@id=\"{0}\"]", elementId);

        }


        int indexInParent = 1;
        GeckoNode siblingNode = node.PreviousSibling;

        while (siblingNode != null)
        {

            if (siblingNode.LocalName == node.LocalName)
            {
                indexInParent++;
            }
            siblingNode = siblingNode.PreviousSibling;
        }


        return String.Format("{0}/{1}[{2}]", GetSmallXpath(node.ParentNode), node.LocalName, indexInParent);
    }

    public static string GetXpath(this GeckoNode node)
    {
        if (node.NodeType == NodeType.Attribute)
        {

            return String.Format("{0}/@{1}", GetXpath(((GeckoAttribute)node).OwnerDocument), node.LocalName);
        }
        if (node.ParentNode == null)
        {

            return "";
        }


        int indexInParent = 1;
        GeckoNode siblingNode = node.PreviousSibling;

        while (siblingNode != null)
        {

            if (siblingNode.LocalName == node.LocalName)
            {
                indexInParent++;
            }
            siblingNode = siblingNode.PreviousSibling;
        }

        return String.Format("{0}/{1}[{2}]", GetXpath(node.ParentNode), node.LocalName, indexInParent);
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25357478

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档