You can find elements by CSS selectors in Selenium by utilizing the find_element
and find_elements
methods and the By.CSS_SELECTOR
argument.
find_element
returns the first occurence of the CSS selector being used, while find_elements
returns all elements of the page that match the selector. And By.CSS_SELECTOR
simply tells Selenium to use the CSS selector matching method.
Here is some example code which navigates to ScrapingBee website and extracts some data using CSS selectors:
from selenium import webdriver
from selenium.webdriver.common.by import By
DRIVER_PATH = '/path/to/chromedriver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
# Open Scrapingbee's website
driver.get("http://www.scrapingbee.com")
# Get the first h1 element using find_element
first_h1 = driver.find_element(By.CSS_SELECTOR, "h1")
# Get all p elements using find_elements
all_p_elements = driver.find_elements(By.CSS_SELECTOR, "p")
# Get all a elements with the class of btn
all_btn_elements = driver.find_elements(By.CSS_SELECTOR, "a.btn")
# Get the textContent of the h1 element
h1_value = first_h1.text
# The total number of p elements on the page
p_total = len(all_p_elements)
# The total number of a.btn elements on the page
btn_elements_count = len(all_btn_elements)
print(h1_value)
print(p_total)
print(btn_elements_count)
You can find some additional examples of CSS selector usage in this article .