William Roberts
William Roberts

Reputation: 329

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>

Upvotes: 4

Views: 11672

Answers (2)

alecxe
alecxe

Reputation: 473833

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);
});

Upvotes: 13

Tom Nijs
Tom Nijs

Reputation: 3950

To find elements on a web page, Protractor uses locators. Read up on them here: https://github.com/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();

Upvotes: 1

Related Questions