从部分获取完整的 XPath [英] Get full XPath from partial

查看:35
本文介绍了从部分获取完整的 XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 perl 中使用 selenium 并在页面上有标签,要访问此标签,我有以下 xpath://*[text()='some here'] ,需要的问题要获得此元素的完整 xpath,例如 /html/body/table/tr/..../any other/and other/ ,是否有任何 selenium 方法或 perl 函数?寻找 perl 解决方案或任何其他有效的东西.

谢谢

解决方案

这个 XSLT 1.0 转换为 $pNode 参数中包含的每个节点生成一个 XPath 表达式:

<xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:strip-space elements="*"/><xsl:template match="node()|@*"><路径><xsl:call-template name="buildPath"/></路径><xsl:apply-templates select="node()|@*"/></xsl:模板><xsl:template name="buildPath"><xsl:variable name="pNode" select="."/><xsl:variable name="theResult"><xsl:for-each select="$pNode"><xsl:variable name="theNode" select="."/><xsl:for-each select="$theNode|$theNode/ancestor-or-self::node()[..]"><xsl:element name="slash">/</xsl:element><xsl:when test="self::*"><xsl:元素名称=节点名称"><xsl:value-of select="name()"/><xsl:variable name="thisPosition" select=计数(前兄弟::*[名称(当前())=名称()])"/><xsl:variable name="numFollowing" select=计数(以下兄弟姐妹::*[名称(当前())=名称()])"/><xsl:if test="$thisPosition + $numFollowing > 0"><xsl:value-of select="concat('[', $thisPosition +1, ']')"/></xsl:if></xsl:element></xsl:when><xsl:否则><!-- 该节点不是元素--><xsl:when test="count(. | ../@*) = count(../@*)"><!-- 属性--><xsl:元素名称=节点名称"><xsl:value-of select="concat('@',name())"/></xsl:element></xsl:when><xsl:when test="self::text()"><!-- 文本--><xsl:元素名称=节点名称"><xsl:value-of select="'text()'"/><xsl:变量名="thisPosition"select="count(preceding-sibling::text())"/><xsl:variable name="numFollowing"select="count(following-sibling::text())"/><xsl:if test="$thisPosition + $numFollowing > 0"><xsl:value-of select="concat('[', $thisPosition +1, ']')"/></xsl:if></xsl:element></xsl:when><xsl:when test="self::processing-instruction()"><!-- 处理指令--><xsl:元素名称=节点名称"><xsl:value-of select="'processing-instruction()'"/><xsl:变量名="thisPosition"select="count(preceding-sibling::processing-instruction())"/><xsl:variable name="numFollowing"select="count(following-sibling::processing-instruction())"/><xsl:if test="$thisPosition + $numFollowing > 0"><xsl:value-of select="concat('[', $thisPosition +1, ']')"/></xsl:if></xsl:element></xsl:when><xsl:when test="self::comment()"><!-- 评论--><xsl:元素名称=节点名称"><xsl:value-of select="'comment()'"/><xsl:变量名="thisPosition"select="count(preceding-sibling::comment())"/><xsl:variable name="numFollowing"select="count(following-sibling::comment())"/><xsl:if test="$thisPosition + $numFollowing > 0"><xsl:value-of select="concat('[', $thisPosition +1, ']')"/></xsl:if></xsl:element></xsl:when><!-- 命名空间:--><xsl:当测试="计数(. | ../命名空间::*)=count(../namespace::*)"><xsl:variable name="apos">'</xsl:variable><xsl:元素名称=节点名称"><xsl:value-of select="concat('namespace::*','[local-name() = ', $apos, local-name(), $apos, ']')"/></xsl:element></xsl:when></xsl:选择></xsl:否则></xsl:选择></xsl:for-each><!-- <xsl:text>&#xA;</xsl:text>--></xsl:for-each></xsl:变量><xsl:value-of select="$theResult"/></xsl:模板></xsl:stylesheet>

应用于以下 XML 文档时:

<div class="asset-header"><h2 class="asset-name entry-title"><a rel="bookmark" href="http://blahblah.com/paper-scissors">剪纸</a>

<div class="asset-content entry-content"><div class="asset-body"><p>纸和剪刀</p>

结果是文档中每个节点的 XPath 表达式列表:

/div<path>/div/@id</path><path>/div/@class</path><path>/div/div[1]</path><path>/div/div[1]/@class</path><path>/div/div[1]/h2</path><path>/div/div[1]/h2/@class</path><path>/div/div[1]/h2/a</path><path>/div/div[1]/h2/a/@rel</path><path>/div/div[1]/h2/a/@href</path><path>/div/div[1]/h2/a/text()</path><path>/div/div[2]</path><path>/div/div[2]/@class</path><path>/div/div[2]/div</path><path>/div/div[2]/div/@class</path><path>/div/div[2]/div/p</path><path>/div/div[2]/div/p/text()</path>

I am using selenium with perl and have label on page, to access this label i have following xpath: //*[text()='some here'] , the problem that a need to get full xpath of this element, like /html/body/table/tr/..../any other/and other/ , is there is any selenium method or perl function ? looking for perl solution or any other working things.

thanks

解决方案

This XSLT 1.0 transformation produces an XPath expression for every node contained in the $pNode parameter:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <path>
    <xsl:call-template name="buildPath"/>
  </path>
  <xsl:apply-templates select="node()|@*"/>
 </xsl:template>


<xsl:template name="buildPath">
 <xsl:variable name="pNode" select="."/>
  <xsl:variable name="theResult">
    <xsl:for-each select="$pNode">
    <xsl:variable name="theNode" select="."/>
    <xsl:for-each select=
    "$theNode
    |
     $theNode/ancestor-or-self::node()[..]">
      <xsl:element name="slash">/</xsl:element>
      <xsl:choose>
        <xsl:when test="self::*">
          <xsl:element name="nodeName">
            <xsl:value-of select="name()"/>
            <xsl:variable name="thisPosition" select=
            "count(preceding-sibling::*
                   [name(current())
                   =
                    name()])"/>
            <xsl:variable name="numFollowing" select=
             "count(following-sibling::
                     *[name(current())
                     =
                       name()])"/>
            <xsl:if test="$thisPosition + $numFollowing > 0">
              <xsl:value-of select=
              "concat('[', $thisPosition +1, ']')"/>
            </xsl:if>
          </xsl:element>
        </xsl:when>
        <xsl:otherwise> <!-- This node is not an element -->
          <xsl:choose>
            <xsl:when test="count(. | ../@*) = count(../@*)">
            <!-- Attribute -->
              <xsl:element name="nodeName">
                <xsl:value-of select="concat('@',name())"/>
              </xsl:element>
            </xsl:when>
            <xsl:when test="self::text()">  <!-- Text -->
              <xsl:element name="nodeName">
                <xsl:value-of select="'text()'"/>
                <xsl:variable name="thisPosition"
                          select="count(preceding-sibling::text())"/>
                <xsl:variable name="numFollowing"
                          select="count(following-sibling::text())"/>
                <xsl:if test="$thisPosition + $numFollowing > 0">
                  <xsl:value-of select=
                  "concat('[', $thisPosition +1, ']')"/>
                </xsl:if>
              </xsl:element>
            </xsl:when>
            <xsl:when test="self::processing-instruction()">
            <!-- Processing Instruction -->
              <xsl:element name="nodeName">
                <xsl:value-of select="'processing-instruction()'"/>
                <xsl:variable name="thisPosition"
                   select="count(preceding-sibling::processing-instruction())"/>
                <xsl:variable name="numFollowing"
                    select="count(following-sibling::processing-instruction())"/>
                <xsl:if test="$thisPosition + $numFollowing > 0">
                  <xsl:value-of select=
                  "concat('[', $thisPosition +1, ']')"/>
                </xsl:if>
              </xsl:element>
            </xsl:when>
            <xsl:when test="self::comment()">   <!-- Comment -->
              <xsl:element name="nodeName">
                <xsl:value-of select="'comment()'"/>
                <xsl:variable name="thisPosition"
                         select="count(preceding-sibling::comment())"/>
                <xsl:variable name="numFollowing"
                         select="count(following-sibling::comment())"/>
                <xsl:if test="$thisPosition + $numFollowing > 0">
                  <xsl:value-of select=
                  "concat('[', $thisPosition +1, ']')"/>
                </xsl:if>
              </xsl:element>
            </xsl:when>
            <!-- Namespace: -->
            <xsl:when test=
              "count(. | ../namespace::*)
              =
               count(../namespace::*)">

              <xsl:variable name="apos">'</xsl:variable>
              <xsl:element name="nodeName">
                <xsl:value-of select="concat('namespace::*',
                '[local-name() = ', $apos, local-name(), $apos, ']')"/>

              </xsl:element>
            </xsl:when>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
    <!-- <xsl:text>&#xA;</xsl:text> -->
  </xsl:for-each>
 </xsl:variable>
 <xsl:value-of select="$theResult"/>
</xsl:template>
</xsl:stylesheet>

when applied on the following XML document:

<div id="entry-1" class="item-asset asset hentry">
  <div class="asset-header">
    <h2 class="asset-name entry-title">
      <a rel="bookmark" href="http://blahblah.com/paper-scissors">Paper Scissors</a>
    </h2>
  </div>
  <div class="asset-content entry-content">
    <div class="asset-body">
     <p>Paper and scissors</p>
    </div>
  </div>
</div>

the result is a list of the XPath expressions for every node in the document:

<path>/div</path>
<path>/div/@id</path>
<path>/div/@class</path>
<path>/div/div[1]</path>
<path>/div/div[1]/@class</path>
<path>/div/div[1]/h2</path>
<path>/div/div[1]/h2/@class</path>
<path>/div/div[1]/h2/a</path>
<path>/div/div[1]/h2/a/@rel</path>
<path>/div/div[1]/h2/a/@href</path>
<path>/div/div[1]/h2/a/text()</path>
<path>/div/div[2]</path>
<path>/div/div[2]/@class</path>
<path>/div/div[2]/div</path>
<path>/div/div[2]/div/@class</path>
<path>/div/div[2]/div/p</path>
<path>/div/div[2]/div/p/text()</path>

这篇关于从部分获取完整的 XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆