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?
XPath text matching that survives real markup. On live pages contains(.) found the element in 98% of cases, text() forms in 67-69%. This guide shows why.
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 clicks, XPath for extraction. Python benchmarks: lxml XPath beats CSS 2x on direct paths, and one extra // costs 570x.
Write selectors against pages that actually loaded
HasData fetches blocked or rendered targets, so your selectors have something to query.