Get XPath of an element tag

A

anupamjain

I am trying to 'grab' the XPath of an element as soon as the user
clicks on it. So I am putting a span tag around that element (it's
actually a <td>) and onmouseover it gets highlighted, now I want that
onmouseclick, I grab the XPath of that td tag. Is there a built in
method to do this in Xpath API (for eg. can I pass the span 'id' to a
function and get the Xpath?)

It's a JSP page where I will be importing the Java file where I will be
using the XPath API.

Also, if there's a better way to do all that I am doing, please let me
know.

Thanks,
Anupam
 
M

Mike Schilling

I am trying to 'grab' the XPath of an element as soon as the user
clicks on it. So I am putting a span tag around that element (it's
actually a <td>) and onmouseover it gets highlighted, now I want that
onmouseclick, I grab the XPath of that td tag. Is there a built in
method to do this in Xpath API (for eg. can I pass the span 'id' to a
function and get the Xpath?)

It's a JSP page where I will be importing the Java file where I will be
using the XPath API.

If I'm understanding you and the result is something like

<span id="uniqueID">
<td/>
</span>

you can use the expression

//span[@id="uniqueID"]/td
 
A

anupamjain

Yeah, but the span tag is 'dynamically' used to highlight the cell. So
if the user comes to that page again, he wont see the span tag at
all.(because I add the span tags before displaying it to the user). So
i want to grab the entire Xpath without using span in between, so if
the user comes that page again, i can retrieve the Xpath (from a
database for e.g) and trace back to that tag.
 
M

Mike Schilling

Yeah, but the span tag is 'dynamically' used to highlight the cell. So
if the user comes to that page again, he wont see the span tag at
all.(because I add the span tags before displaying it to the user). So
i want to grab the entire Xpath without using span in between, so if
the user comes that page again, i can retrieve the Xpath (from a
database for e.g) and trace back to that tag.

Oh. In that case, it's harder. You need to navigate via parent nodes to
the document element, at each node figuring out how many previous sibling
nodes there are with the same name, and generate an XPath like

/level1/level2[2]/level3[1]/level4[1]/td[12]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top