7

Can someone please suggest if there is any way to search for an element with exact text while using cssContainingText. I am using below code but have multiple values in dropdown as below.

Value 1
Value 1 Test 1
Value 1 Protractor 1

Below is the code I am using.

element(by.cssContainingText('option', Value1)).click();
NewWorld
  • 764
  • 1
  • 10
  • 31

2 Answers2

6

You can solve this with cssContainingText as well if you use a regexp with an exact match for the second argument, e.g.:

element(by.cssContainingText('option', /^Value 1$/))

See related SO post here: Match whole string

matsr
  • 4,302
  • 3
  • 21
  • 36
4

The by.cssContainingText() is a partial match locator by definition. If you want to have an exact match, you can solve it with an XPath expression:

element(by.xpath('//option[. = "Value 1"]')).click();

And, since you are dealing with select and options, see if this wrapper would be a good fit:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks. Xpath worked well for now so I will go with that but will also try abstraction at some point. – NewWorld Nov 02 '16 at 14:07