How to find elements by CSS selector in Selenium

CSS selectors are one of the most popular ways to parse HTML pages when web scraping. Using Selenium, to find elements by CSS selectors we can use driver.find_element() and driver.find_elements() methods:

from selenium import webdriver
from import By

driver = webdriver.Chrome()

element = driver.find_element(By.CSS_SELECTOR, 'p')
# then we can get the element text
"Availing himself of the mild, summer-cool weather that now reigned in these latitudes..."
# we can also get tag name and attributes:

# for multiple elements we need to iterate
for element in driver.find_elements(By.CSS_SELECTOR, 'p'):


Also see: How to find elements by XPath in Selenium

Question tagged: Selenium, Css Selectors, Data Parsing

Related Posts

How to Use Chrome Extensions with Playwright, Puppeteer and Selenium

In this article, we'll explore different useful Chrome extensions for web scraping. We'll also explain how to install Chrome extensions with various headless browser libraries, such as Selenium, Playwright and Puppeteer.

Intro to Web Scraping using Selenium Grid

In this guide, you will learn about installing and configuring Selenium Grid with Docker and how to use it for web scraping at scale.

How to Scrape Google Maps

We'll take a look at to find businesses through Google Maps search system and how to scrape their details using either Selenium, Playwright or ScrapFly's javascript rendering feature - all of that in Python.