4

Is it possible to get/generate the XPath, for a text selected in the HTML page. Say,I have opened an html file using a browser,say safari/firefox, now i select some text (using mouse), i want to generate/get the xpath for that. is it possible to get it programatically? how?

ganapati
  • 625
  • 2
  • 12
  • 24
  • I asked a similar question and got some interesting answers: http://stackoverflow.com/questions/3454526/how-to-calculate-the-xpath-position-of-an-element-using-javascript – Marc Aug 11 '10 at 13:06

1 Answers1

3

Some years ago I wrote the following sample and tested it to work with Mozilla and with Opera. It stores the selection in a cookie and can restore the selection from that cookie. It generates and stores an XPath expression to the startContainer and endContainer of the selected range and the numbers of the offset.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • I can load the sample http://home.arcor.de/martin.honnen/javascript/storingSelection1.html fine and it continues to work for me with Mozilla browsers like Firefox, it also works in Microsoft Edge. It does not work in Internet Explorer as that browser does not support XPath selection on nodes in HTML documents. – Martin Honnen Feb 12 '16 at 15:07