gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:15:09 25 4
gpt4 key购买 nike

我有以下 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”元素的值,我如何才能获得更新链接的 XPath?例如,如果我知道卡号是“5123 4567 8901 2346”,我怎样才能得到链接元素 "<a href="/admin?cpm_id=905">" 的 XPath?

评论更新

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?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5227330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com