1

How would you get the following tag using CSS?

<p>You can only use the text inside the tag</p>

As in xpath I'd use the following:

//p[contains(text(), "inside the tag")

PS: I can't close the xpath, it tries to auto complete with code... :S

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
user104966
  • 41
  • 4

2 Answers2

4

I believe CSS3 selectors can only filter attributes, not the text within tags. So you could do something like a[href~="aspx"] to match links to aspx pages, but that's as far as content-based matching can go.

For what you want to do, you'll probably have to use javascript or server-side processing.

Have a look at quirksmode and W3 for more information.

John G
  • 3,483
  • 1
  • 23
  • 12
  • I'm almost sure that css selector exists. Thanks for the recommendations but I'd like to use the CSS selector as a Selenium locator, so only pure CSS can be used... – user104966 May 14 '09 at 19:19
3

This is what I was looking for!

p:contains("inside the tag")
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
user104966
  • 41
  • 4
  • @mateusza: As of today, none of them do. `:contains()` was dropped from the CSS3 spec and we haven't seen it return in the CSS4 drafts. jQuery and Selenium *do* implement it, though. See [this answer](http://stackoverflow.com/a/4781167/106224). Since the OP said he's using Selenium, this will work. It just won't work in a stylesheet. – BoltClock Dec 21 '11 at 10:56