Last active
January 14, 2021 10:42
-
-
Save florianpasteur/4911931835104099f13e9d969c67dddd to your computer and use it in GitHub Desktop.
Find html element by text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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