While creating Selenium automation tests sometimes we need to retrieve a web element with JavaScript, not with Selenium driver.findElement
.
So, I know we can do something like
javaScript = "document.getElementsByClassName('myClassName')[0].click();"
driver.execute_script(javaScript)
I see we can locate elements in this way ByClassName
, ByName
, ByTagName
and BytagNameNS
but in most cases element can be uniquely located with CSS Selector or XPath only while I couldn't see such way in documentations and tutorials.
So, I'm wondering is it possible to locate web elements with JavaScript by XPath or CSS Selectors?