🚀 We are hiring! See open positions

How to find HTML elements by class?

When web scraping, the most common way to navigate HTML data is to find elements by class name. For that, we can use CSS or XPath Selectors:

CSS selectors

  • Use the .class notation, which will find any nodes that contain full class name:
    javascript
    `.some-class`
    will match:
    `<a class="some-class"></a>`
    `<a class="first some-class third"></a>`
  • Use the [class*="<partial>"] notation which will find any nodes that contain a given string:
    javascript
    `[class*="some-class"]`
    will match:
    `<a class="some-class"></a>`
    `<a class="first some-class third"></a>`

Xpath selectors

Alternatively, XPath selectors can be used with similar functions:

  • //*[@class="link"] will find any element where the class is exactly equal to "link"
  • //*[contains(@class, "link")] will find any element where class contains the string "link"
Scale Your Web Scraping
Anti-bot bypass, browser rendering, and rotating proxies — all in one API. Start with 1,000 free credits.
No credit card required 1,000 free API credits Anti-bot bypass included
Not ready? Get our newsletter instead.