[Greasemonkey] generating XPath string from dom node
Al
anevare1 at yahoo.com
Fri Mar 17 00:02:57 EST 2006
thanks everyone.. Brilliant. Just amazing the response and community around greasemonkey. Greasemonkey rules. Really makes working on web apps super interesting.
only 1 minor update I had to make to the latest suggestion.. for xpath.. offset starts at 1, not 0.. so only altered this line:
var offset = 0;
replaced with
var offset =1; and all works perfectly.
Thanks!
Al
----- Original Message ----
From: Jonathan Buchanan <jonathan.buchanan at gmail.com>
To: greasemonkey at mozdev.org
Sent: Thursday, March 16, 2006 8:40:38 AM
Subject: Re: [Greasemonkey] generating XPath string from dom node
Naturally, I didn't realise that until about 5 seconds after I'd hit Send :-)
Since we're going right to the root, I suppose the double forward
slash could be omitted as well?
function createXPath(el)
{
var path = [tagNameWithOffset(el)];
var parent = el.parentNode;
while (parent.nodeType == 1)
{
path.push(tagNameWithOffset(parent));
parent = parent.parentNode;
}
return "/" + path.reverse().join("/");
}
function tagNameWithOffset(el)
{
var offset = 0;
var sibling = el.previousSibling;
while (sibling)
{
if (sibling.tagName == el.tagName)
{
offset++;
}
sibling = sibling.previousSibling;
}
return el.tagName + "[" + offset + "]";
}
On 3/16/06, Jeremy Dunck <jdunck at gmail.com> wrote:
> On 3/16/06, Jonathan Buchanan <jonathan.buchanan at gmail.com> wrote:
> > Further to Matt's suggestion, if you want to select a specific
> > element, you'll need the offset for the case where there are multiple
> > elements with the same path. Something like:
> >
> ...
> > return "//" + tagNames.reverse().join("/") + "[" + offset + "]";
>
> You'll want to find the offset for each tag, not just the leaf.
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
>
_______________________________________________
Greasemonkey mailing list
Greasemonkey at mozdev.org
http://mozdev.org/mailman/listinfo/greasemonkey
More information about the Greasemonkey
mailing list