🚀 We are hiring! See open positions

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:

python
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://httpbin.dev/html")

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

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

driver.close()

Also see: how-to-find-elements-by-xpath-in-selenium

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.