Skip to content

Instantly share code, notes, and snippets.

@bspavel
Last active March 5, 2019 09:14
Show Gist options
  • Save bspavel/895af8030a6298ef09760ea76198c9b9 to your computer and use it in GitHub Desktop.
Save bspavel/895af8030a6298ef09760ea76198c9b9 to your computer and use it in GitHub Desktop.
document.getElementByXpath
//https://stackoverflow.com/questions/12689768/how-can-i-extend-the-document-object
//https://stackoverflow.com/questions/10596417/is-there-a-way-to-get-element-by-xpath-using-javascript-in-selenium-webdriver
if (typeof HTMLDocument !== 'undefined') {
HTMLDocument.prototype.getElementByXpath=function (path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
};
} else {
Document.prototype.getElementByXpath=function (path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
};
}
console.log( document.getElementByXpath("//html[1]/body[1]/div[1]") );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment