findElementByText

abstract suspend fun findElementByText(text: String, bestMatch: Boolean = false, returnEnclosingElement: Boolean = true): Element?

Finds and returns the first element containing the specified text, or the best match if bestMatch is true.

Return

The found Element, or null if no matching element is found.

Parameters

text

The text to search for within elements.

bestMatch

If true, finds the closest match based on length, which is more expensive and slower. This is useful when searching for common terms (e.g., "login") to get the most relevant element, such as a login button, instead of unrelated elements containing the text.

returnEnclosingElement

If true, returns the enclosing element of the found text node.