27
votes

Hey I have this code in one of my div elements:

<div class="col-sm-8">Account Information: </div>

Can someone tell me how I would go about finding this element in my protractor code? Is it possible to do something like this:

expect(element(by.divText('Account Information: ')).isDisplayed()).toBe(true);

I have multiple elements with the class "col-sm-8" so I am not able to find the element by class. I was just wondering if there is any way to possibly find the element using the text in the div element? Thanks for the help!

3

3 Answers

53
votes

I would recommend you to use by.cssContainingText

element(by.cssContainingText('.col-sm-8', 'Account Information'))
6
votes

There is no webdriver method which would allow locating an element by its text. You could try using xpath in the following way (not tested):

element(by.xpath('//div[contains(text(), "Account Information: ")]')
2
votes

keep in mind by.cssContainingText matches the element by PARTIAL text

so element(by.cssContainingText('div', 'male')) will actually match both male and female text

To solve this, use xpath with exact text match

element(by.xpath('//div[text()="male"]'))