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!

2
  • duplicate stackoverflow.com/questions/15364298/… Commented Sep 17, 2014 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 Commented Sep 17, 2014 at 14:09

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

yes, i know that :) i have that xpath in my question, but i was querin about css locator, due to it's 'faster', etc..
I missed that :) But it seems like CSS Selectors doesn't support what you're trying to do. Read the highest voted comment here: stackoverflow.com/questions/16788310/…
So then you're question is answered :) I would be grateful if you marked it as such :)
Votin up, because maybe someday someone will come up with solution :)
What's up with people straight-up assuming that a CSS selector would be faster than XPath? This is the second time 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)?
|
0

You could try a regex for exact matching.

var r = /^a$/;
css=a:contains((r.test('Text')) ? 'Text' : '');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.