Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Webpage Element Information

by erroneousBollock (Curate)
on Nov 14, 2007 at 23:00 UTC ( #650864=note: print w/replies, xml ) Need Help??

in reply to Webpage Element Information

There is a Firefox extension, WebDeveloper. It has one menu item, outline current element. It shows position of element. Example:
"html > body #id-479 > center > table > tbody > tr > td .main_content > form > table > tbody > tr > td > ul[0]" 
for an element in this page.
Firstly, please note that that is not a unique position. If you want to be able to find what was selected/clicked later, you need to use an XPath-like notation; something like:

"html > body#id-479 > center[0] > table[0] > tbody [0] > tr[1] > td.main_content > form[0] > table[0] > tbody[0] > tr[0] > td[0] > ul"

Secondly, roughly the same question was asked by user2000 recently in Position on Web Page. The conclusions are:

  • You need to know Javascript and the W3C DOM very well to figure this out.
  • There are many problems doing this across different browsers.
  • It's 100x harder if the user can click a TextElement (rather than just Type3 DOM nodes)
  • It's 1000x harder if the user can make a text selection spanning multiple DOM nodes.

And that's just the content-targeting part. The remainder is communicating the "path" string to your perl back-end via AJAX (XMLHttpRequest, etc).


I see that like YourMother, I assumed you'd want to remember the XPath-like position in order to translate it to a (browser-relative) pixel-position.

If that's not the case, then you can start with something like YourMother has written below (using jquery, or some other library that smooths over browser differences).

To be accurate, you will need to form an XPath-like expression, so why not use real XPath?

There's a cross-platform library available here.

To construct the XPath expression, you'll have to walk up the DOM from the target element to the document.body (root). At each level, you'll need to determine 'id', 'class' and sibling-rank (with that selection priority).


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://650864]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2020-08-06 07:33 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (39 votes). Check out past polls.