1

I wonder if I could match an element by exact text via css-locators (The overall goal is to use it as a locator for Selenium). I know the contains method

For example:

<div><div>S - Subscriber</div></div>

I can retrieve it by css=div:contains('Subscriber') or by css=div:contains('S - Subscriber') but I need exact match. I tried css=div:contains('^S - Subscriber$') and this element is not found.

One more point:

When we use xpath we can assume that given text is in exact element by retrieving text with text() function (for example //div[text()='S - Subscriber'] will point us exactly to the child div, whenever //div[contains(., 'S - Subscriber')] will point us to the parent div, if i am not messed up), so is there equivalent in css another way then css=div > div:contains('S - Subscriber') ? I mean pointing just one element, without parent div.

Thanks in advance!

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
German Petrov
  • 1,475
  • 1
  • 17
  • 21
  • duplicate http://stackoverflow.com/questions/15364298/make-jquerys-contains-select-only-exact-string – Teo Sep 17 '14 at 13:50
  • 1
    i don't need it in jquery, I need css-selector to use it in Selenium. So css=div[text='S - Subscriber'] - not found, css=div[text()='S - Subscriber'] - not found, can you advise how to build it to use as element description for Selenium? Updated the question, but i thought tags will be informative – German Petrov Sep 17 '14 at 14:09

2 Answers2

3

Since you're using selenium I would just go with using Xpath instead. You'll get the element you want with the following: browser.find_element_by_xpath('//div[text()="S - Subscriber"]')

Sadly you can't use css selectors for what you're trying to do.

Jonathan
  • 8,453
  • 9
  • 51
  • 74
  • yes, i know that :) i have that xpath in my question, but i was querin about css locator, due to it's 'faster', etc.. – German Petrov Sep 17 '14 at 15:00
  • I missed that :) But it seems like CSS Selectors doesn't support what you're trying to do. Read the highest voted comment here: http://stackoverflow.com/questions/16788310/what-is-the-difference-between-css-selector-xpath-which-is-betteraccording-t – Jonathan Sep 17 '14 at 15:13
  • So then you're question is answered :) I would be grateful if you marked it as such :) – Jonathan Sep 17 '14 at 15:37
  • Votin up, because maybe someday someone will come up with solution :) – German Petrov Sep 17 '14 at 15:41
  • What's up with people straight-up assuming that a CSS selector would be faster than XPath? This is the [second time](http://stackoverflow.com/questions/25867922/selecting-elements-by-name-with-wildcards#comment40482150_25867922) I've heard it this week. So what if it's faster if there isn't even a solution? Are you going to refuse to use a readily-available alternative *just because it's slower* (than a *non-existent* solution for crying out loud)? – BoltClock Sep 17 '14 at 15:54
  • @BoltClock, sorry, but where have you read that i am refusing to use xpath here? I AM using it, but i was wondering if i could do the same with css. No reason to be critic here. – German Petrov Sep 18 '14 at 14:30
  • and yes, if something does not exist today, doesn't mean that it will not occur tomorrow, when somebody will open this question and tell "hey mate, this is how you can do it" – German Petrov Sep 18 '14 at 14:39
0

You could try a regex for exact matching.

var r = /^a$/;
css=a:contains((r.test('Text')) ? 'Text' : '');
Teo
  • 614
  • 1
  • 4
  • 13