title: Locate web elements with JavaScript by CSS Selector or XPath?
body: <p>While creating Selenium automation tests sometimes we need to retrieve a web element with JavaScript, not with Selenium <code>driver.findElement</code>.<br />
  So, I know we can do something like</p>
  <pre><code>javaScript = &quot;document.getElementsByClassName('myClassName')[0].click();&quot;
  driver.execute_script(javaScript)
  </code></pre>
  <p>I see we can locate elements in this way <code>ByClassName</code>, <code>ByName</code>, <code>ByTagName</code> and <code>BytagNameNS</code> but in most cases element can be uniquely located with CSS Selector or XPath only while I couldn't see such way in <a href="https://developer.mozilla.org/en-US/docs/Web/API/Document" rel="nofollow noreferrer">documentations</a> and <a href="https://www.w3schools.com/jsref/met_document_getelementbyid.asp" rel="nofollow noreferrer">tutorials</a>.<br />
  So, I'm wondering is it possible to locate web elements with JavaScript by XPath or CSS Selectors?</p>