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. This means we can select any element in the HTML DOM based on its attribute value with XPath.

To find elements by attribute value, the name of the attribute can be used in [@attribute=value] predicate syntax or using contains() function for a partial match like so: [contains(@attribute, "value")].

Here are some interactive examples to demonstrate this:

  1. We can select the attribute value itself using @ syntax:
<!-- 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>
  1. Or filter elements by attribute value 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>

Provided by Scrapfly

This knowledgebase is provided by Scrapfly — a web scraping API that allows you to scrape any website without getting blocked and implements a dozens of other web scraping conveniences. Check us out 👇