How to select elements by class in XPath?

To select elements by class using XPath we can match the @class attribute using contains() function or the = operator.

For example, to select <a class="link"></a> we could use //a[@class="link"] or //a[contains(@class, "link")] selectors. See this interactive example:

<html> <a class="ignore"></a> <a class="link">website</a> <a class="blue link underline">website 2</a> </html>

Note that using contains() might match partial matches. For example, disabled-link would be matched by our contains(@class, "link") selector.
To match by a single class we can use contains(concat(" ", normalize-space(@class), " "), " match ") pattern:

<html> <a class="ignore"></a> <a class="link">website</a> <a class="blue link underline">website 2</a> <a class="disabled-link underline">ignore</a> </html>

Tip: If you're using Python's parsel package then there's an equivalent shortcut has-class(). For example, //a[has-class("link")]

Question tagged: XPath, Data Parsing

Related Posts

Web Scraping With Ruby

Introduction to web scraping with Ruby. How to handle http connections, parse html files for data, best practices, tips and an example project.

Parsing HTML with Xpath

Introduction to xpath in the context of web-scraping. How to extract data from HTML documents using xpath, best practices and available tools.

Web Scraping With PHP 101

Introduction to web scraping with PHP. How to handle http connections, parse html files for data, best practices, tips and an example project.