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)
I had an empty string yielded from the promise getText() returned, neither from an
input
nor atextarea
Here is an excerpt of my HTML:While this yielded an empty string:
that did the trick:
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
ortextarea
, it is withgetText()
ofp
in a ng-repeater. How can I usegetAttribute('value')
when there is value bound to any of the elements ?getInnerHtml()
returnsbut I want “Text2” only.
@KamalakannanPE do you run your tests in PhantomJS? If so, using
getInnerHtml()
instead ofgetText()
did solve our problem (see #1229).@juliemr @sallojusuresh using
getAttribute('value')
actually doesn’t help, since the example doesn’t use aninput
element. @KamalakannanPE tried to explain thatgetText()
is expected to return the innerHTML of thep
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:
getText()
would then give me:(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: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. usinggetInnerHtml
instead ofgetText
works though in this situation.@jamlen if you do create a new issue, please do link it here.