How to select element with one of many names in XPath?

XPath allows interacting with any element's attribute such as class, id, href and any other through the @ syntax.
Attribute values then can be used in predicates using = or contains(). See these interactive examples:

Select attribute value, like the urls of <a> links:

<!-- select all links by selecting the @href attributes --> <html> <a href="/categories/1">category</a> <a href="/product/1">product 1</a> <a href="/product/2">product 2</a> <a href="/product/3">product 3</a> </html>

Or filter the elements by attribute using contains() function:

<!-- select only product links by checking @href attribute --> <html> <a href="/categories/1">category</a> <a href="/product/1">product 1</a> <a href="/product/2">product 2</a> <a href="/product/3">product 3</a> </html>
Question tagged: XPath

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.