CSS selectors let you find HTML elements by tag name, class, ID, attributes, or their position in the page structure.
In Selenium with Python, use find_element() to return the first matching element or find_elements() to return all matching elements. Pass By.CSS_SELECTOR as the locator strategy and the CSS selector as the second argument.
Prerequisites
You need:
- Python 3
- Selenium
- A supported browser such as Chrome
If Selenium is not installed, run:
python3 -m pip install selenium
Find CSS Selectors with Browser Developer Tools
To find an element using Selenium, you need its CSS selector. Use your browser's developer tools to inspect the element and get the selector quickly.
In Chrome:
- Right-click the element and select Inspect.
- Find the selected element in the HTML tree.
- Right-click it and select Copy > Copy selector.
For a more detailed walkthrough, see ScrapingBee's guide on extracting CSS selectors using Chrome.
Find an Element by CSS Selector
Use find_element() when you want the first element that matches a CSS selector.
The following example opens the ScrapingBee website and finds the first <h1> element. Save it as test.py:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://www.scrapingbee.com/")
heading = driver.find_element(By.CSS_SELECTOR, "h1")
print(heading.text)
finally:
driver.quit()
Run the script:
python3 test.py
The script prints the text of the first <h1> element:
The Best Web Scraping API to Avoid Getting Blocked
If you inspect the ScrapingBee homepage in your browser's developer tools, you can locate the same element here:

Here, "h1" is the CSS selector. Selenium returns the first matching <h1> element on the page. The current Selenium API defines By.CSS_SELECTOR specifically for locating elements that match a CSS selector.
Find Multiple Elements by CSS Selector
Use find_elements() when several elements can match the selector.
For example, the following code finds all paragraph elements:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://www.scrapingbee.com/")
paragraphs = driver.find_elements(By.CSS_SELECTOR, "p")
print(len(paragraphs))
finally:
driver.quit()
Run the script:
python3 test.py
The script prints the number of <p> elements currently found on the homepage:
54
find_elements() returns all matching elements instead of only the first one.
Find an Element by Class
You can find an element by its class with a CSS selector. Prefix the class name with a period (.) and pass the selector to find_element().
For example, the ScrapingBee homepage contains this element:
<p class="mb-[36px] text-[20px]">
![Chrome DevTools showing the paragraph element with the class mb-[36px] text-[20px] on the ScrapingBee homepage](/uploads/selenium-css-selector-class-devtools.png)
Because the class name contains square brackets, they must be escaped in the CSS selector:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://www.scrapingbee.com/")
element = driver.find_element(
By.CSS_SELECTOR,
".mb-\\[36px\\]"
)
print(element.text)
finally:
driver.quit()
The output is the content of the element with that class:
Scrape websites with our powerful web scraping API, without managing proxies, browsers, or anti-bot defenses. Fetch pages, leverage AI extraction, or integrate with dedicated APIs for LLM, RAG, and analytics pipelines.
Find an Element by ID
To find an element by its id, prefix the ID value with #.
For example, the ScrapingBee homepage contains this section:
<section id="faqs">

The CSS selector is #faqs. Use it with By.CSS_SELECTOR to locate the section:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://www.scrapingbee.com/")
element = driver.find_element(
By.CSS_SELECTOR,
"#faqs"
)
print(element.find_element(By.TAG_NAME, "h2").text)
finally:
driver.quit()
Run the script:
python3 test.py
The script prints the heading inside the FAQ section:
Web Scraping API FAQs
Find an Element by Attribute
To find an element by an attribute, use this CSS selector format:
[attribute="value"]
For example, to find a link whose href contains /pricing, you can use a[href*="/pricing"]. The *= operator means the attribute value contains the specified text.
Use it with By.CSS_SELECTOR:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://www.scrapingbee.com/")
element = driver.find_element(
By.CSS_SELECTOR,
'a[href*="/pricing"]'
)
print(element.get_attribute("href"))
finally:
driver.quit()
Run the script:
python3 test.py
The script prints the value of the link's href attribute. Note that get_attribute("href") returns the absolute URL, even though the page markup uses a relative one:
https://www.scrapingbee.com/pricing/
Find Elements Using Partial Attribute Values
CSS selectors provide several operators for matching part of an attribute value:
[href^="/"] starts with /
[href*="pricing"] contains pricing
[href$=".pdf"] ends with .pdf
For example, to find all links whose href starts with /:
links = driver.find_elements(
By.CSS_SELECTOR,
'a[href^="/"]'
)
Find Nested Elements with CSS Selectors
You can combine selectors to locate elements inside other elements:
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
try:
driver.get("https://www.scrapingbee.com/")
element = driver.find_element(
By.CSS_SELECTOR,
"section#faqs h2"
)
print(element.text)
finally:
driver.quit()
The selector section#faqs h2 finds an <h2> anywhere inside the <section> with id="faqs".
Run the script:
python3 test.py
Output:
Web Scraping API FAQs
Alternatives to Selenium
Selenium is useful for browser automation or for interacting with JavaScript-rendered pages.
For static HTML, you can use BeautifulSoup for parsing or Scrapy for larger scraping projects.
ScrapingBee has a practical BeautifulSoup tutorial covering HTML parsing and CSS selectors.
For larger crawling and scraping projects, see ScrapingBee's Scrapy tutorial.