Como obter o xpath clicando em um elemento html
Eu sou novo em programação e tenho que gerar um Xpath ao clicar em um elemento html. por exemplo: se eu tiver clicado na caixa de texto do nome de usuário, então ele deve me dar o xpath como html / head / body / tr [1] / table [2] ..... etc. etc. O principal é que eu não posso usar o firebug como meu aplicativo está indo completamente para executar no IE. Eu vi muito fxn para obter xpath e tentei integrá-lo, mas não estou recebendo o valor de retorno. Um trecho de código simples, onde eu usei jquery click () função para recuperar o valor não está funcionando.A coisa é que eu sou incapaz de passar o elemento html na função.A função xpath eu tirei apenas deste site. Meu código está abaixo.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>click demo</title>
<style>
p
{
color: red;
margin: 5px;
cursor: pointer;
}
p:hover
{
background: yellow;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js">
</script>
</head>
<body>
<p id ="test">First Paragraph</p>
<p>Second Paragraph</p>
<p>Yet one more Paragraph</p>
<script>
$( "#test" ).click(function() { var value= $(this).getXPath();
alert(value) });
function getXPath( element )
{
var val=element.value;
alert("val="+val);
var xpath = '';
for ( ; element && element.nodeType == 1; element = element.parentNode )
{
alert(element);
var id = $(element.parentNode).children(element.tagName).index(element) + 1;
id > 1 ? (id = '[' + id + ']') : (id = '');
xpath = '/' + element.tagName.toLowerCase() + id + xpath;
}
return xpath;
}
</script>
</body>
</html>