title: How to fetch the WebElement object upon manually clicking an object within an active WebDriver Session
body: <p>I am creating an application using java swing for verification of Object repository which would help maintain selenium scripts. This app would launch a WebDriver instance and then let the user navigate manually to the element which he/she wishes to inspect.</p>
  
  <p>I want to get instance of the WebElement object which the user clicks so that i can pass the last clicked web element as the argument within below function which returns an Xpath.</p>
  
  <p>I couldn't find anything related to this in web.</p>
  
  <pre><code>public String getElementXPath(WebDriver driver, WebElement element) {
      return (String)((JavascriptExecutor)driver).executeScript("gPt=function(c){if(c.id!==''){return'id(\"'+c.id+'\")'}if(c===document.body){return c.tagName}var a=0;var e=c.parentNode.childNodes;for(var b=0;b&lt;e.length;b++){var d=e[b];if(d===c){return gPt(c.parentNode)+'/'+c.tagName+'['+(a+1)+']'}if(d.nodeType===1&amp;&amp;d.tagName===c.tagName){a++}}};return gPt(arguments[0]).toLowerCase();", element);
  }
  </code></pre>