HTML XPath-如何根据另一个元素的值获取一个元素的XPath? [英] HTML XPath - How to get the XPath of an element based on the value of another element?

查看:167
本文介绍了HTML XPath-如何根据另一个元素的值获取一个元素的XPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML代码:

I have the following HTML code:

<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'元素的值,如何获取更新链接的XPath?例如,如果我知道卡号为"5123 4567 8901 2346",如何获取链接元素"<a href="/admin?cpm_id=905">"的XPath?

My question is if I know the value of the 'Card No' element, how can I get the XPath of the Update link? For example, If I know the Card No is "5123 4567 8901 2346", how can I get the XPath of the link element "<a href="/admin?cpm_id=905">"?

根据评论更新

我正在使用自动化测试工具 称为QTP.我需要获得XPath 更新链接元素以识别它 在网页上根据的价值 信用卡号.我在追求什么 是为了获得这样的XPath 为"/html/body/table/tbody/tr [3]/td [1]/a". 但是,这是 更新链接元素.我想要 能够基于 信用卡号.

I'm using an automation test tool called QTP. I need to get the XPath of the update link element to identify it on the webpage based on the value of the Credit Card number. What I'm after is to get the XPath such as"/html/body/table/tbody/tr[3]/td[1]/a". However, this is the static path of the update link element. I would like to be able to get the XPath based on a Credit Card number.

推荐答案

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

当此转换应用于提供的XML文档时:

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

产生了所需的正确结果:

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

这篇关于HTML XPath-如何根据另一个元素的值获取一个元素的XPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆