Skip to content

Instantly share code, notes, and snippets.

@florianpasteur
Last active January 14, 2021 10:42
Show Gist options
  • Save florianpasteur/4911931835104099f13e9d969c67dddd to your computer and use it in GitHub Desktop.
Save florianpasteur/4911931835104099f13e9d969c67dddd to your computer and use it in GitHub Desktop.
Find html element by text
function findElement(text: string, searchStart: Node = document.body): HTMLElement {
return document
.evaluate(
"//*[text()='" + text + "']",
searchStart,
null,
XPathResult.ANY_TYPE,
null
)
.iterateNext() as HTMLElement;
}
// Untyped Version (Browser copy paste use)
function findElement(text, searchStart = document.body) {
return document
.evaluate(
"//*[text()='" + text + "']",
searchStart,
null,
XPathResult.ANY_TYPE,
null
)
.iterateNext();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment