How to select HTML elements by text using CSS Selectors?

There used to be a way to select HTML elements by text using CSS Selectors by making use of :contains(text). However, this has been deprecated for a long time and is no longer supported by the W3C standard. If you want to select an element by text, you should look into other options. Most Python libraries provide a way for you to do so.

For instance, you can select an element by text using XPath Selectors in Selenium like this:

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("https://www.scrapingbee.com")

# Match the h1 tag that contains the word "Tired"
h1 = driver.find_element(By.XPATH, "//h1[contains(text(), 'Tired')]")

Alternatively, if you are using BeautifulSoup then you can do something like this:

import re
import requests
from bs4 import BeautifulSoup

# Open Scrapingbee's website
html = requests.get("https://scrapingbee.com").text
soup = BeautifulSoup(html)

# Match the h1 tag that contains the word "Tired"
h1 = soup.find("h1", text=re.compile("Tired"))

print(h1)
# Output: <h1 class="mb-33">Tired of getting blocked while scraping the web?</h1>

Related CSS Selectors web scraping questions: