从部分 XPath 获取完整 XPath

发布于 2024-09-30 07:09:36 字数 234 浏览 8 评论 0原文

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肤浅与狂妄 2024-10-07 07:09:36

寻找 Perl 解决方案或任何其他解决方案
工作事物

此 XPath 2.0 表达式:

string-join(for $node in ancestor-or-self::node()
            return concat(('@')[$node/self::attribute()],
                          $node/name(),
                          (concat('[',
                                  count($node/preceding-sibling::node()
                                           [name()=$node/name()]) + 1,
                                  ']'))[$node/../node()
                                           [name()=$node/name()][2]]),
            '/')

编辑:更短的表达式。

looking for perl solution or any other
working things

This XPath 2.0 expression:

string-join(for $node in ancestor-or-self::node()
            return concat(('@')[$node/self::attribute()],
                          $node/name(),
                          (concat('[',
                                  count($node/preceding-sibling::node()
                                           [name()=$node/name()]) + 1,
                                  ']'))[$node/../node()
                                           [name()=$node/name()][2]]),
            '/')

Edit: Shorter expression.

笑梦风尘 2024-10-07 07:09:36

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

<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>
</xsl:text> -->
  </xsl:for-each>
 </xsl:variable>
 <xsl:value-of select="$theResult"/>
</xsl:template>
</xsl:stylesheet>

应用于以下 XML 文档时

<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>

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

<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>

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>
</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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文