HTML XPath - How to get an XPath element based on the value of another element?

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> 

My question is, do I know the value of the No Map element, how can I get the XPath link from Update? For example, if I know that the card number is "5123 4567 8901 2346", how can I get the XPath of the link element "<a href="/admin?cpm_id=905">" ?

Update from comments

I use an automation testing tool called QTP. I need to get an XPath update link element to identify it on a web page based on the value of a credit card number. What I am after is to get 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 get XPath based on my credit card number.

+2
source share
3 answers

This is not an easy 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 conversion is applied to 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 desired, correct result is output:

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

Dimitre's answer is good for XSLT solution, but just for completeness, here is another perspective. Since the data is in a table with a single structure, in a sense, XPath for any one element is the same when you consider the card number as a parameter. For example, in C #, the string.Format method uses {0} as a placeholder, as shown in XPATH_TEMPLATE below. Just fill in the space in this template with the card number to get the required XPath expression.

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

The above gives this XPath expression:

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

So, regardless of whether you work in XSLT or C # or something else, this XPath flavor is more significant (for example, / table / tbody / tr [3] / td [1] / a), since it is autonomous with contextual information.

0
source

Go through a concept called bookmarklets. Using the specified bookmarklets in your HTML code, the result is / HTML / BODY / TABLE / TBODY / TR [3] / TD [1] / A.

URL Firebug's equivalent of "Copy XPath" in Internet Explorer?

Bookmarking the name and email editing blocks on this page creates

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

Hope this helps

0
source

All Articles