Skip to content

Finding HTML Elements by Class in Python

When you need a particular element in Python, its class attribute is often the most direct handle.

OUTPUT FORMATSHTMLJSONMarkdown

The right technique depends on how you obtained the page: parsing static HTML calls for one approach, while driving a browser calls for another. This guide compares the two captured workflows so you can pick the one that fits your setup.

Pick the approach that matches how you fetch the page

Start by deciding how the HTML reaches your script. If you already have the page markup as text, BeautifulSoup is the natural fit. If you need a real browser to load the page, Selenium is the fit. That single decision determines whether you use a class_ argument or an XPath expression.

Decision checklist:

  • Do you have HTML as a string? Choose BeautifulSoup (module code0001).
  • Do you need browser rendering or interaction? Choose Selenium (module code0002).
  • Do you need one element or all of them? Use find / find_element for one, find_all for many.
  • Is the class value exact and stable? Verify it in the markup first; class names can change without notice.

BeautifulSoup for parsed HTML, Selenium for a live browser

With BeautifulSoup, pass the class name through the class_ argument. Use find when you expect a single match and find_all when several elements may share the class. The captured example searches for an h1 carrying the class mb-[33px] and returns the full element markup.

With Selenium, the same target is reached through an XPath expression that tests whether the class attribute contains that value, then find_element returns the first match and its text is printed. Note the failure distinction: BeautifulSoup parses markup you supply, so it cannot render JavaScript-driven content, whereas Selenium drives a browser and can, but requires a configured driver path.

Worked example: same target, different output

Both workflows aim at the same element: an h1 with class mb-[33px]. The BeautifulSoup example prints the element itself, including its tags, while the Selenium example prints only the visible text. If your output shows the tags, you used the first approach; if it shows plain text, you used the second. Use that difference to confirm which code path actually ran.

Before adapting either example, replace the placeholder page address with your target URL and, for Selenium, set the driver path to your local chromedriver location. Then check that the class token still exists on the page, since a renamed class will make both selectors return nothing.

Related topics

Once class-based selection works, the next steps are broadening the selector style or matching by visible text instead. Those related topics cover using CSS selectors more generally in Python and selecting elements by their text content.

Frequently asked questions

Which method should I use for one element versus many?

Use BeautifulSoup with find for a single match or find_all for every match, passing the class name through class_. With Selenium, use an XPath expression that checks the class attribute, as shown in module code0002.

Does the library change the class-matching approach?

The choice follows the library already in your workflow. BeautifulSoup suits parsing fetched HTML, while Selenium suits browser-driven retrieval. Both examples target the same h1 element and class value, but they obtain the page differently.

How can I verify my selector is correct?

Confirm the exact class token in the live markup before running either example. In the captured example, the target class is mb-[33px] on an h1 element, and the expected text is 'Tired of getting blocked while scraping the web?'