Reputation:
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!
Upvotes: 31
Views: 38671
Reputation: 8948
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"]'))
Upvotes: 3
Reputation: 8900
I would recommend you to use by.cssContainingText
element(by.cssContainingText('.col-sm-8', 'Account Information'))
Upvotes: 55
Reputation: 4061
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: ")]')
Upvotes: 7