我有以下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 回答
This is not a difficult task for XSLT :
when this transformation is applied on the provided XML document:
the wanted, correct result is produced:
Dimitre的答案适用于XSLT解决方案,但为了完整性,这是另一个视角 . 由于数据位于具有统一结构的表中,因此在某种意义上,当您将卡号视为参数时,任何一个元素 is the same 的XPath . 例如,在C#中,
string.Format
方法使用{0}
作为占位符,如下面的XPATH_TEMPLATE所示 . 只需使用卡号填写此模板中的占位符即可获得必需的XPath表达式 .上面得到了这个结果的XPath表达式:
因此,无论您是使用XSLT还是C#还是其他东西,这种XPath都更有意义(例如/ table / tbody / tr [3] / td [1] / a),因为它是自包含的上下文信息 .
超越了名为bookmarklets的概念 . 使用HTML代码上的给定bookmarklet生成结果/ HTML / BODY / TABLE / TBODY / TR [3] / TD [1] / A.
该网址是Equivalent of Firebug's "Copy XPath" in Internet Explorer?
在此页面上的名称和电子邮件编辑框上执行书签会生成
希望这可以帮助