Logo New Black

Mastering Selenium: How to Find Elements by CSS Selectors – A Comprehensive Guide

CSS selectors are a powerful tool in the world of web development, enabling developers to navigate through and manipulate HTML documents with precision. When paired with Selenium, a browser automation framework, CSS selectors unlock a new level of efficiency in finding elements on a web page. The methods driver.find_element() and driver.find_elements() are pivotal for anyone looking to perform web scraping or automate web interactions, as they allow for the identification of web elements based on their CSS properties. This technique not only streamlines the process of web scraping but also ensures more accurate and reliable results. For those aiming to elevate their scraping projects, leveraging a web scraping API can provide a comprehensive suite of tools designed to optimize the extraction of web data, making the process smoother and more effective.

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()

For additional information, see: How to find elements by XPath in Selenium