protractor: getText() not working on sub element - p tag, returns empty string

Hi, I have the below block in my ng-repeat

 <div ng-repeat="node in nodes">
     <div class="class1">
            <div class="class2">
                  <span class="cls-icon"></span>
                  <p>Text1</p>
            </div>
   </div>
</div>
 <div ng-repeat="node in nodes">
     <div class="class1">
            <div class="class2">
                  <span class="cls-icon"></span>
                  <p>Text2</p>
            </div>
   </div>
</div>
.....

Since the only text inside each repeated block exists in p tag, I’m using like below,

element(by.repeater('node in nodes').row(0)).getText().then(function(text) {
    expect(text).toBe('Text1');
});

But this always returns empty string ‘’.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 25 (4 by maintainers)

Most upvoted comments

I had an empty string yielded from the promise getText() returned, neither from an input nor a textarea Here is an excerpt of my HTML:

<li data-ng-repeat="emitter in account.identifier.emitters" data-ng-if="emitter._id != account.emitter._id">
    <a href="" data-ng-click="switchToEmitter(emitter)" data-ng-bind="emitter.name"></a>
</li>

While this yielded an empty string:

element(by.css('li[data-ng-repeat="emitter in account.identifier.emitters"] a')).getText()

that did the trick:

element(by.css('li[data-ng-repeat="emitter in account.identifier.emitters"] a')).getAttribute('innerText')

Closing since this is an answered question. Thanks @sallojusuresh!

Hi,

If you could go through the FAQ link: http://angular.github.io/protractor/#/faq, you can find that ‘getText()’ returns empty string. so use “element.getAttribute(‘value’)”

Why would getText() return an empty string while getAttribute(‘textContent’) returns the expected text? I feel like I can’t trust getText and need to use getAttribute all the time now.

Why did you close this issue ? The issue is not with input or textarea, it is with getText() of p in a ng-repeater. How can I use getAttribute('value') when there is value bound to any of the elements ?

getInnerHtml() returns

<div class="class1">
            <div class="class2">
                  <span class="cls-icon"></span>
                  <p>Text2</p>
            </div>
   </div>

but I want “Text2” only.

@KamalakannanPE do you run your tests in PhantomJS? If so, using getInnerHtml() instead of getText() did solve our problem (see #1229).

@juliemr @sallojusuresh using getAttribute('value') actually doesn’t help, since the example doesn’t use an input element. @KamalakannanPE tried to explain that getText() is expected to return the innerHTML of the p element. So, I also wouldn’t regard this issue to be closed, but #1229 seems to be the better place to work on this issue.

I ran into this as well. I don’t have a nice repro, as it happened when I tried to get the text from an ACE editor. But invariably the text that was missing was the text within internal HTML elements that were not visible due to falling outside of the bounds of the ACE editor box. Example:

This is <i>all</i> visible.
This is a longer line, and the second sentence is beyond the visible area. The text in tags is <i>not visible</i>.

getText() would then give me:

This is all visible.
This is a longer line, and the second sentence is beyond the visible area. The text in tags is .

(The not visible piece is missing from the second line.)

I checked the DOM and the elements were all there. The workaround with getAttribute('innerText') works perfectly.

Is it possible to reopen this issue? The first given answer does not apply here, and using innerHTML does not do the job when needing to compare texts, regardless of additional tags.

I have the same issue, and I need to manually remove HTML tags and transform HTML entities back to their text value to get my tests passing. Current implementation of getText does not work consistently as described in the docs:

Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.

I am using protractor getting null while using binding. Please advice. Thanks

Error : Expected null to equal ‘xyz’; element(by.binding(‘ctrl.formData.branchAddressLine1’)).getAttribute(‘value’).then(function (value) { expect(value).toEqual(‘xyz’); });.

I have the exact same issue here. Using phantonjs 1.9.19. I’m trying to extract the text of a th element, and a blank string is returned. using getInnerHtml instead of getText works though in this situation.

@jamlen if you do create a new issue, please do link it here.