Skip to content

Instantly share code, notes, and snippets.

@bozdoz
Created April 25, 2021 03:57
Show Gist options
  • Save bozdoz/ba727932a1e89bdfa5e9ecf05193d009 to your computer and use it in GitHub Desktop.
Save bozdoz/ba727932a1e89bdfa5e9ecf05193d009 to your computer and use it in GitHub Desktop.
JavaScript document XPath text selector
function getElemByText(text) {
return document.evaluate(
`//*[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '${text}')]`,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment