13

Question about automation. I use Selenium RC.

I upgraded my FF to 3.6.18 and it seems that it can't find any locators which have "contains()" inside

I've read alot of forums. It's a known problem, but I did not find any solution.

So what to use instead of contains() now?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Pasha
  • 131
  • 1
  • 1
  • 3
  • 1
    `:contains()` never made it into the spec, see http://stackoverflow.com/questions/4781141/why-h3nth-child1containsa-selector-doesnt-work/4781167#4781167 – BoltClock Jul 11 '11 at 11:37
  • You could pick up XPath, which has a `contains()` function. – BoltClock Jul 11 '11 at 11:40
  • @BoltClock: while it is true that `:contains()` never made it into the CSS3 spec, according to the latest published [Selenium docs](http://release.seleniumhq.org/selenium-core/1.0.1/reference.html), "Currently the css selector locator supports **all** css1, css2 and css3 selectors except... [the exceptions are unrelated; omitted for space reasons here]". So _in Selenium_ it is supposed to work. – Michael Sorens Jul 12 '11 at 17:19
  • @Paul Sweatte: This has nothing to do with CSS4... – BoltClock Aug 06 '12 at 19:39
  • @msorens: Sorry I'm over a year late, but you're right; Selenium RC does implement `:contains()` according to how it was defined before being dropped. – BoltClock Aug 06 '12 at 19:43
  • @BoltClock Thanks for the update. Is this question Selenium specific, not XPath or CSS? – Paul Sweatte Aug 06 '12 at 19:47
  • @Paul Sweatte: It looks Selenium specific, yes. I also notice that you're adding the [css4] tag to questions with answers that mention CSS4, which doesn't seem right. Remember that tags should describe a *question*, not its answers. If a question does not specifically ask if there's a CSS4 proposal for something, the tag probably isn't appropriate. – BoltClock Aug 06 '12 at 19:50
  • @BoltClock Sorry, I wasn't sure about that. Thanks for the clarification. I'll revert my changes. – Paul Sweatte Aug 06 '12 at 20:02
  • @Paul Sweatte: No problem. Also see [this meta post](http://meta.stackexchange.com/questions/109121/proper-use-of-tags-to-enhance-or-only-to-describe-question/109127#109127). – BoltClock Aug 06 '12 at 20:16
  • http://stackoverflow.com/questions/1520429/css-3-content-selector – jantimon Jan 20 '14 at 09:21
  • @MichaelSorens 2019 and XPath is still useful and still the most powerful. – DBagBaggerWithSwagger Jul 14 '19 at 03:19

2 Answers2

12

The only functionality in CSS which is similar to contains() is:

element[attribute*="substring"]

It is helpful when you need to check some substring which contains in the attribute.

1

You can use jQuery :contains(text)

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

<script>
    $( "div:contains('foo')" ).css( "text-decoration", "underline" );
</script>
bklups
  • 300
  • 1
  • 13