Knowledge Base
Ways to Find an Element by Text with Selenium
A practical guide to locating elements by their visible text in Selenium, handling whitespace and partial matches, and where proxies fit in automation.
Sometimes the only stable way to identify an element is by the text a user actually sees, such as a button labelled "Add to cart" or a link that reads "Next". Selenium can target elements by text, mainly through XPath, which makes it a handy fallback when IDs and classes are missing or unreliable.
This walkthrough explains the practical ways to find elements by text, the whitespace and case pitfalls that cause misses, and where proxies become relevant when running such automation broadly.
Finding elements by text with XPath
The usual approach uses XPath. Two patterns cover most needs:
- An exact match selects an element whose text equals a specific string, conceptually
//button[text()='Submit']. - A partial match uses
contains(text(), 'Sub')to match elements whose text includes a fragment, which is more forgiving.
Partial matching is often safer in practice, because real pages frequently wrap text in nested spans or pad it with whitespace that breaks exact matches.
Handling whitespace, case, and nested text
Visible text often carries leading or trailing whitespace, line breaks, or is split across child elements. The bare text() function only reads the direct text of a node, so it can miss text held in child spans. Using the normalize-space() function trims and collapses whitespace, and matching against the combined string content rather than direct text helps when labels are wrapped in inner tags.
Text comparisons are also case-sensitive by default. If casing varies, prefer a contains match on a stable fragment, or normalise the case in your XPath, to avoid brittle exact comparisons.
Making text locators more robust
Text can change with translations, A/B tests, or content updates, so text-based locators are inherently a little fragile. To reduce breakage:
- Combine text with a tag or attribute, for example a link whose text contains a fragment, to narrow the match.
- Prefer a stable ID or data attribute when one is available, and fall back to text only when necessary.
- Wait for the element to be present before searching, since text rendered by JavaScript may not exist on initial load.
Treat text locators as a useful complement to attribute-based ones rather than your only strategy.
Where proxies fit in automation
Locating elements by text is a local operation, but the automation around it often visits many pages or runs in parallel. At that point per-IP limits can interrupt runs, and proxies help by spreading requests across addresses.
For broad automation, datacenter proxies are a value-focused option, while residential proxies may suit location-sensitive or stricter targets. See our provider comparison to weigh coverage and concurrency.
What to compare before buying
Before you order, weigh these points so the proxies you pick match your real workload and budget:
- Proxy type: datacenter for general runs, residential for sensitive targets
- Concurrency support matching the number of parallel sessions
- Geographic coverage so localised text variants appear correctly
- Session stability for multi-step, logged-in flows
- Bandwidth model appropriate for full browser rendering
- Transparent pricing: confirm the exact package before ordering
- Support quality for resolving blocks during long runs
Frequently asked questions
Use an XPath that compares the node's text to a specific string, such as targeting a button whose text equals 'Submit'. Exact matches are precise but break when extra whitespace or nested spans are present.
Use the XPath contains function on the text, which matches elements whose text includes a fragment. This is more forgiving than an exact match for real-world markup.
Hidden whitespace, line breaks, or text split across child spans are common culprits. Wrapping the comparison in normalize-space and matching combined content rather than direct text usually fixes it.
They are useful when no stable ID exists, but text can change with translations or content edits. Combine text with a tag or attribute, and prefer stable IDs when available.
No, locating elements is local. Proxies become useful when the surrounding automation visits many pages or runs in parallel and risks hitting per-IP rate limits.
Datacenter proxies are a value-focused choice for general automation, while residential proxies may be more suitable for location-aware or stricter targets.
Related pages worth comparing
Have a comparison question about how to find element by text using selenium? Email info@comparebestproxy.com.