protractor how to get text from div?

12,242

Target all messages available inside the container

var messageElms = $('.container').all(by.binding('message'));

Assuming you expect only 1 message:

expect(messageElms.count()).toBe(1);

Assuming you expect that Hello to be the first message:

expect(messageElms.get(0).getText()).toEqual('Hello this is it!');
Share:
12,242
Pindakaas
Author by

Pindakaas

Smooth as peanutbutter but hard as chocolate.

Updated on June 14, 2022

Comments

  • Pindakaas
    Pindakaas almost 2 years

    Trying to get the text inside my div:

    <div class="ng-scope ng-binding">
             Hello this is it!
    </div>
    

    In my jasmine script I have (using xpath):

    var helloTxt=driver.findElement(by.xpath('//*[@id="top"]/div/div/div[3]/div[2]/div/div[1]/div[1]/div/div'));
    expect(helloTxt.getText()).toBe('Hello this is it!');
    

    the code looks like this:

    <div class="container">
            <div data-ng-repeat="message in getMessages() track by $index">
                {{message}}
            </div>
    </div>
    

    How can I check get the messages in {{message}}? Is there another way to access this div without using xpath? Or how can I fix this?