🚀 We are hiring! See open positions

How to select elements by text in XPath?

by scrapecrow Oct 28, 2022

To select elements by text using XPath we can either match the text() value or use it in a contains() function.
For example, to select <a>websites</a> we would use //a[contains(text(), "website")] selector. See this interactive example:

<html> <a>ignore</a> <a>website</a> <a>WEBSITE</a> </html>

Note that contains() method is case sensitive.
For case-insensitive selections we can use matches (sometimes called re:test()) function:

<html> <a>ignore</a> <a>website</a> <a>WEBSITE</a> </html>

Related Articles