首页 文章

HTML XPath - 如何根据另一个元素的值获取元素的XPath?

提问于
浏览
2

我有以下HTML代码:

<table>
<tbody>
    <tr>
        <th class="colhead" nowrap="nowrap">Update</th>
        <th class="colhead" nowrap="nowrap">Card No</th>
    </tr>
    <tr>
        <td nowrap="nowrap"><a href="/admin?cpm_id=1043">
        Update</a></td>
        <td nowrap="nowrap">4987 6543 2109 8769</td>
    </tr>
    <tr>
        <td nowrap="nowrap"><a href="/admin?cpm_id=905">
        Update</a></td>
        <td nowrap="nowrap">5123 4567 8901 2346</td>
    </tr>
</tbody>

我的问题是,如果我知道'Card No'元素的值,我怎样才能获得Update链接的XPath?例如,如果我知道卡号是"5123 4567 8901 2346",我如何获得链接元素 "<a href="/admin?cpm_id=905">" 的XPath?

Update from comments

我正在使用名为QTP的自动化测试工具 . 我需要获取更新链接元素的XPath,以根据信用卡号的值在网页上识别它 . 我想要的是得到XPath,如“/ html / body / table / tbody / tr [3] / td [1] / a” . 但是,这是更新链接元素的静态路径 . 我希望能够根据信用卡号码获得XPath .

3 回答

  • 0

    This is not a difficult task for XSLT

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pCardNo" select=
     "'5123 4567 8901 2346'"/>
    
     <xsl:template match="/">
      <xsl:variable name="vNode" select=
      "/*/*/tr[td[2] = $pCardNo]/td[1]
                         /a/ancestor-or-self::*"/>
    
      <xsl:apply-templates select="$vNode" mode="path"/>
     </xsl:template>
    
     <xsl:template match="*" mode="path">
      <xsl:value-of select="concat('/',name())"/>
    
      <xsl:variable name="vnumPrecSiblings" select=
        "count(preceding-sibling::*[name()=name(current())])"/>
    
      <xsl:variable name="vnumFollSiblings" select=
        "count(following-sibling::*[name()=name(current())])"/>
    
      <xsl:if test="$vnumPrecSiblings or $vnumFollSiblings">
       <xsl:value-of select=
          "concat('[', $vnumPrecSiblings +1, ']')"/>
      </xsl:if>
     </xsl:template>
    
    </xsl:stylesheet>
    

    when this transformation is applied on the provided XML document:

    <table>
        <tbody>
            <tr>
                <th class="colhead" nowrap="nowrap">Update</th>
                <th class="colhead" nowrap="nowrap">Card No</th>
            </tr>
            <tr>
                <td nowrap="nowrap">
                    <a href="/admin?cpm_id=1043">         Update</a>
                </td>
                <td nowrap="nowrap">4987 6543 2109 8769</td>
            </tr>
            <tr>
                <td nowrap="nowrap">
                    <a href="/admin?cpm_id=905">         Update</a>
                </td>
                <td nowrap="nowrap">5123 4567 8901 2346</td>
            </tr>
        </tbody>
    </table>
    

    the wanted, correct result is produced:

    /table/tbody/tr[3]/td[1]/a
    
  • 0

    Dimitre的答案适用于XSLT解决方案,但为了完整性,这是另一个视角 . 由于数据位于具有统一结构的表中,因此在某种意义上,当您将卡号视为参数时,任何一个元素 is the same 的XPath . 例如,在C#中, string.Format 方法使用 {0} 作为占位符,如下面的XPATH_TEMPLATE所示 . 只需使用卡号填写此模板中的占位符即可获得必需的XPath表达式 .

    string XPATH_TEMPLATE = 
      "//td[normalize-space(.)='{0}']/preceding-sibling::td/a";
    string CardNumber = "4987 6543 2109 8769";
    string XPathExpression = string.Format(XPATH_TEMPLATE, CardNumber);
    

    上面得到了这个结果的XPath表达式:

    //td[normalize-space(.)='4987 6543 2109 8769']/preceding-sibling::td/a
    

    因此,无论您是使用XSLT还是C#还是其他东西,这种XPath都更有意义(例如/ table / tbody / tr [3] / td [1] / a),因为它是自包含的上下文信息 .

  • 1

    超越了名为bookmarklets的概念 . 使用HTML代码上的给定bookmarklet生成结果/ HTML / BODY / TABLE / TBODY / TR [3] / TD [1] / A.

    该网址是Equivalent of Firebug's "Copy XPath" in Internet Explorer?

    在此页面上的名称和电子邮件编辑框上执行书签会生成

    //INPUT[@id='display-name']
    
    //INPUT[@id='m-address']
    

    希望这可以帮助

相关问题