XPath Contains Text: How and When to Use It

Introduction to XPath
XPath, or XML Path Language, is a powerful tool for navigating and querying elements within an XML or HTML document. It’s widely used in web scraping and test automation to locate specific elements on a web page. One useful feature of XPath is the contains() function, which helps find elements based on partial text matches—a practical approach when content is dynamic or partially known.
What Does contains() in XPath Do?
The contains()
function is a flexible way to locate elements without specifying the exact text, making it ideal for targeting elements containing variable or dynamic content. Instead of a full text match, contains()
allows you to use a portion of the text, especially useful when dealing with elements that may have changing content or appended text.
Basic Syntax of XPath Contains Text
To use contains()
in XPath, here’s the general syntax:
//tagname[contains(text(), 'partial_text')]
In this syntax:
tagname
is the HTML tag of the element you want to locate (e.g., button, div, span).- 'partial_text' represents the part of the text you want to match within the element.
Practical Examples
- Locate a Button by Text Suppose you want to select a button element containing the text "Submit" but aren’t sure if it says "Submit Form" or "Submit Now." You can use:
//button[contains(text(), 'Submit')]
- Match Text Within a Specific Attribute Sometimes, you may want to match partial text within an attribute rather than within the visible text itself. For instance:
//div[contains(@class, 'example')]
Here, any div element whose class name includes "example" will be selected.
- Combine Contains with Additional Filters You can chain other conditions with contains() for more specific queries:
//a[contains(@href, 'page') and contains(text(), 'Click')]
This will select any anchor <a>
element with an href containing "page" and text containing "Click."
Why Use contains() in XPath?
The contains()
function in XPath offers a unique advantage: it enables matching with flexibility. Here are some reasons to use contains()
:
- Dynamic Text: Perfect for sites where text may vary slightly across elements.
- Efficiency: Reduces the need for complex, absolute paths.
- Versatility: Useful in applications like web scraping and automated testing where content changes often.
Use Cases of XPath Contains Text
- Web Scraping: Scraping scripts can use
contains()
to reliably target elements with frequently updated text. - Automated Testing: Test scripts benefit from
contains()
by locating elements with slight text variations, making tests more adaptable. - Content Verification: Validating the presence of partial text within elements without relying on exact matches.
Limitations and Considerations
While contains()
is a powerful tool, it has its limitations:
- Broad Matching: Be cautious, as
contains()
may capture more elements than intended, especially if the partial text is common. - Performance Impact: Using
contains()
extensively can slow down performance on large documents.
Conclusion
XPath’s contains()
function simplifies selecting elements by partial text, offering flexibility across web scraping, automated testing, and other data extraction needs. Whether dealing with dynamic content or targeting multiple elements with shared attributes, contains()
is a vital part of the XPath toolkit.
Table of Contents
Take a Taste of Easy Scraping!
Get started now!
Step up your web scraping
Find more insights here

A Simple Guide to Using Reddit Scrapers for Data Collection
Reddit Scraper automates collecting posts, comments, user metadata, etc., which would be tedious or nearly impossible manually. Below I explain what reddit scrapers are, how they’re commonly used, risks involved, and best practices (especially relevant for someone using MrScraper).

Why Many Scrapers Prefer Using Elite Proxies?
Elite proxies also called high-anonymity proxies do not only hide your real IP address, they also hide the fact that you're using a proxy.

Reddit Scraper: Everything You Need to Know About Extracting Data from Reddit
Reddit scraper is a tool or script designed to collect data from Reddit posts, comments, subreddits, user profiles, and threads—either via official API access or through web scraping techniques.
@MrScraper_
@MrScraper