Is there a CSS selector that matches an element by the text it contains?
No. Standard CSS has no selector for matching elements by their text content. A :contains() pseudo-class was proposed for CSS Selectors Level 3 but was dropped from the spec, and no browser supports it natively. To select by visible text you have to leave CSS. The common routes are tagging the relevant elements with a data-* attribute and matching the attribute instead, querying textContent in JavaScript and applying a class at runtime, or using XPath (//div[contains(text(),'Target')]) inside a scraping or automation tool like Selenium or Playwright. The :contains() syntax you may have seen in jQuery, Cypress, or uBlock Origin is a tool-specific extension and does not work in browser CSS. :has() is broadly supported now but matches by descendant structure, not text, so it does not solve this problem.
Related articles
All articles →How to Select Elements By Text in XPath?
Discover basic and advanced XPath techniques for selecting web elements by text, including contains(), text(), regular expressions, and more.
CSS Selectors Cheat Sheet: BS4, Scrapy, Selenium
CSS selectors from basic to :has() and :is(), with Python code for scraping and automation. Includes a library support table and resilient selector patterns.
XPath vs CSS: Why Web Scrapers Should Stop Listening to QA Testers
Use CSS Selectors for browser automation (clicks) and XPath for data extraction. See Python benchmarks proving XPath is faster in lxml.
Write selectors against pages that actually loaded
HasData fetches blocked or rendered targets, so your selectors have something to query.