I have a span element on my page. I was wondering how I could extract the text in between the span opening tag and span closing tag. I'm using selenium with protractor in javascript.
<span....>
Inner text
</span>
I have a span element on my page. I was wondering how I could extract the text in between the span opening tag and span closing tag. I'm using selenium with protractor in javascript.
<span....>
Inner text
</span>
Share
Improve this question
edited Mar 31, 2015 at 20:22
alecxe
474k127 gold badges1.1k silver badges1.2k bronze badges
asked Mar 31, 2015 at 20:15
William RobertsWilliam Roberts
3292 gold badges6 silver badges21 bronze badges
0
2 Answers
Reset to default 13It is actually called a text of an element. Use getText()
:
var elm = element(by.id("myid"));
expect(elm.getText()).toEqual("My Text");
Note that getText()
as many other methods in protractor returns a promise. expect()
"knows" how to resolve the promise - it would wait until the real text value would be retrieved and only then perform an assertion.
If you want to see an actual text value on the console, resolve the promise with then()
:
elm.getText().then(function (text) {
console.log(text);
});
To find elements on a web page, Protractor uses locators. Read up on them here: https://github./angular/protractor/blob/master/docs/locators.md Since you haven't posted any code, this may differ from the solution you need. The locator I'll use is by using its ID.
var foo = element(by.id('yourspanID')).getText();