Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract inner from a span element in selenium/protractor javascript

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>
like image 987
William Roberts Avatar asked Dec 09 '22 04:12

William Roberts


1 Answers

It 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);
});
like image 103
alecxe Avatar answered Dec 11 '22 09:12

alecxe