Protractor: Finding Element by Div Text

2020-05-30 03:56发布

问题:

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!

回答1:

I would recommend you to use by.cssContainingText

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


回答2:

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: ")]')