title: Selenium - Universal Way to Convert "WebElement" to Javascript or JQuery Object
body: <p>I have a a simple WebElement and I want to perform a series(one or more) of JS/JQuery type actions on my object. </p>
  
  <p>Most of the time this is easy, I simply get the ID of the object, pass that into the "ExecuteScript" function, and viola works great:</p>
  
  <pre><code>RemoteWebDriver driver = ...;
  var element = driver.FindElementByXPath("..."); //Or any other type of lookup
  driver.ExecuteScript(@"$('#" + element.GetAttribute("id") + "')."DoSomeStuff...;");
  </code></pre>
  
  <p>20% this doesn't work at all because the item doesn't have an ID. Of course there are many ways to lookup items in Selenium and many ways to lookup items in jQuery or Javascript, but they don't always map, and while it may be possible to construct a query in jQuery that can lookup a Selenium object, that methodology cannot be the same for every object type. </p>
  
  <p>Searching around it seems most people use the "id" method(Examples: <a href="https://stackoverflow.com/questions/39628677/selenium-finding-element-in-a-javascript-popup">1</a>, <a href="http://executeautomation.com/blog/ijavascriptexecutorselenium/" rel="nofollow noreferrer">2</a>, <a href="https://stackoverflow.com/questions/5490523/selecting-and-identifying-element-with-jquery-to-use-it-in-selenium-2-java-api">3</a>). You can do this the <a href="https://stackoverflow.com/questions/27315761/using-jquery-with-selenium-webdriver-how-to-cast-json-object-to-webelement">opposite way</a>. Another idea would be to give each element a unique ID in selenium before accessing it in jQuery, but it doesn't look like <a href="https://stackoverflow.com/questions/8473024/selenium-can-i-set-any-of-the-attribute-value-of-a-webelement-in-selenium">that would work</a> because to set it you need to using Javascript or jQuery.</p>
  
  <p>I've yet to find a way to do this <strong>Universally</strong> for every element, how can this be done?</p>