Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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).


In reply to Re: Webpage Element Information by erroneousBollock
in thread Webpage Element Information by artist

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (4)
    As of 2020-08-13 18:16 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (74 votes). Check out past polls.