Mastering XPath: Find Elements Containing Keyword

Benjamin Jul 11, 2026

In the realm of web scraping and automation, XPath is an invaluable tool for navigating and extracting data from XML documents, including HTML. When you're looking to extract specific information from a webpage, knowing how to use "contains" with XPath can significantly simplify your task. This article delves into the intricacies of using "contains" in XPath, providing practical examples and tips to enhance your web scraping prowess.

the silhouette of a person is shown in green on a black background with numbers and letters
the silhouette of a person is shown in green on a black background with numbers and letters

Before we dive into the specifics, let's ensure we're on the same page. XPath, or XML Path Language, is a query language used to navigate through elements and attributes in an XML document. It's widely used in web scraping to extract data from HTML pages. Now, let's explore how to use "contains" with XPath.

the silhouette of a person standing in front of a blue background with words all over it
the silhouette of a person standing in front of a blue background with words all over it

Understanding "contains" in XPath

"contains" is a built-in XPath function that checks if a node-set contains a specified string. It returns true if the string is found in any of the nodes, and false otherwise. The syntax is simple: contains(node-set, string). Let's break this down.

๊•ค โŠน  โŒ• ๐ค๐ž๐ฒ๐ฐ๐จ๐ซ๐ ๐“‚ƒ แข..แข
๊•ค โŠน โŒ• ๐ค๐ž๐ฒ๐ฐ๐จ๐ซ๐ ๐“‚ƒ แข..แข

The node-set is the set of nodes you want to search within, and string is the string you're looking for. For example, if you have an HTML element like this:

<div>This is a sample text.</div>

And you want to check if it contains the string "sample", you would use:

Pinterest Keyboards
Pinterest Keyboards

contains(div, 'sample')

Case Sensitivity

By default, the "contains" function is case-sensitive. So, contains(div, 'Sample') would return false for the above example. To make it case-insensitive, you can use the lower-case() or upper-case() functions:

contains(lower-case(div), 'sample')

Using "contains" with "text()"

Pinterest keywords : dark citycore aesthetic โ™ก
Pinterest keywords : dark citycore aesthetic โ™ก

Often, you'll want to check if the text content of an element contains a certain string. To do this, use the "text()" function in combination with "contains". For instance, to check if a paragraph contains the text "web scraping":

contains(paragraph, 'web scraping')

Practical Examples

Now that we've covered the basics, let's look at some practical examples. Suppose we're scraping a webpage with the following HTML structure:

an image of some type of font and numbers
an image of some type of font and numbers

<div>
  <h2>Title</h2>
  <p>This is a paragraph with some text.</p>
  <p>Another paragraph with some more text.</p>
</div>

We want to extract the text from the paragraph that contains the word "text". Here's how you would do it using XPath:

//p[contains(text(), 'text')]/text()

Extracting Multiple Matches

canva element keyword
canva element keyword
Are you a Psychopath? Personality Quiz
Are you a Psychopath? Personality Quiz
an iphone screen with many different types of text and pictures on the phone, including keyboard keys
an iphone screen with many different types of text and pictures on the phone, including keyboard keys
Keywords
Keywords
Master Keyword Research with ChatGPT!
Master Keyword Research with ChatGPT!
an iphone screen with various pictures and captions on it, including the eiffel tower in the background
an iphone screen with various pictures and captions on it, including the eiffel tower in the background
Keywords for an unfiltered aesthetic
Keywords for an unfiltered aesthetic
the screen shot shows how to draw flowers and wine glasses in one drawing app, which is also available on iphone or ipad
the screen shot shows how to draw flowers and wine glasses in one drawing app, which is also available on iphone or ipad
๐–ฅ‰ เฃช .โ”€ฬธ โŸ ๐Ÿง  ยท ๐šฑ๏ธฉ๏ธช๐‘„›ื๐š‚ื…ีฝื๐‘„˜ แฏคฬธ เซฎ โ€ข ๏ปŒ - แƒ โ‹† เฃช.
๐–ฅ‰ เฃช .โ”€ฬธ โŸ ๐Ÿง  ยท ๐šฑ๏ธฉ๏ธช๐‘„›ื๐š‚ื…ีฝื๐‘„˜ แฏคฬธ เซฎ โ€ข ๏ปŒ - แƒ โ‹† เฃช.
๐–ฅ‰ เฃช .โ”€ฬธ โŸ ๐Ÿง  ยท ๐šฑ๏ธฉ๏ธช๐‘„›ื๐š‚ื…ีฝื๐‘„˜ แฏคฬธ เซฎ โ€ข ๏ปŒ - แƒ โ‹† เฃช.
๐–ฅ‰ เฃช .โ”€ฬธ โŸ ๐Ÿง  ยท ๐šฑ๏ธฉ๏ธช๐‘„›ื๐š‚ื…ีฝื๐‘„˜ แฏคฬธ เซฎ โ€ข ๏ปŒ - แƒ โ‹† เฃช.
an anime story is shown in this screenshot with the caption's text below it
an anime story is shown in this screenshot with the caption's text below it
๐˜พ๐™–๐™ฃ๐™ซ๐™–| ๐™†๐™–๐™ฉ๐™– ๐™ ๐™ช๐™ฃ๐™˜๐™ž ๐Ÿ”‘
๐˜พ๐™–๐™ฃ๐™ซ๐™–| ๐™†๐™–๐™ฉ๐™– ๐™ ๐™ช๐™ฃ๐™˜๐™ž ๐Ÿ”‘
โœธ ๐“‚ƒ ๐—ฝษ‘ื๊ญ‘ษ˜ึด  โ‹†  p๐ซstโ•
โœธ ๐“‚ƒ ๐—ฝษ‘ื๊ญ‘ษ˜ึด โ‹† p๐ซstโ•
๐˜พ๐™–๐™ฃ๐™ซ๐™–| ๐™†๐™–๐™ฉ๐™– ๐™ ๐™ช๐™ฃ๐™˜๐™ž ๐Ÿ”‘
๐˜พ๐™–๐™ฃ๐™ซ๐™–| ๐™†๐™–๐™ฉ๐™– ๐™ ๐™ช๐™ฃ๐™˜๐™ž ๐Ÿ”‘
โœจPinterest Keywordsโœจ
โœจPinterest Keywordsโœจ
๊•ค โŠน  โŒ• ๐ค๐ž๐ฒ๐ฐ๐จ๐ซ๐ ๐“‚ƒ แข..แข
๊•ค โŠน โŒ• ๐ค๐ž๐ฒ๐ฐ๐จ๐ซ๐ ๐“‚ƒ แข..แข
an iphone screen showing different types of text
an iphone screen showing different types of text
Pinterest keyword~
Pinterest keyword~
How to find keywords in just 1 minute
How to find keywords in just 1 minute

What if you want to extract all paragraphs that contain a certain word? You can use the "substring()" function to achieve this. Here's how you can extract all paragraphs that contain the word "text":

//p[substring(text(), string-length(text()) - string-length('text') + 1) = 'text']

This XPath expression finds all paragraphs where the last occurrence of the word "text" is at the end of the paragraph. This ensures that we're matching whole-word occurrences.

Tips and Best Practices

Using "contains" with XPath can be a powerful tool, but it's important to use it judiciously. Here are a few tips:

  • Be specific: The more specific you can be with your XPath expressions, the more accurate your results will be.
  • Use "starts-with" and "ends-with": If you're looking for a specific prefix or suffix, use the "starts-with()" and "ends-with()" functions instead of "contains".
  • Test your expressions: Always test your XPath expressions in a safe environment before running them on a live website. Tools like Chrome DevTools or online XPath testers can be invaluable here.

In conclusion, mastering "contains" in XPath is a crucial skill for any web scraper. It allows you to extract data based on partial matches, making your scraping tasks more flexible and efficient. With practice and a bit of creativity, you'll find countless ways to leverage this powerful function in your web scraping projects.