Jatin
Jatin

Reputation: 670

Need to get the data of span using protractor

I want to get the id=2504 which is in html span using protractor and display it in the console log. This id is generated dynamically so the id number can be different all the time.My code looks like :

<span class="link ng-binding" ng-click="openTrackId(mapFeedBack.reportId)">Your tracking number is 2504</span>

Please advise how can i achieve it?

Upvotes: 0

Views: 976

Answers (2)

Haseeb
Haseeb

Reputation: 200

In the span text if the ID is the last thing written you can do

var textTokens = text.split(" ");
var Id = textTokens[textTokens.length - 1];
console.log(Id);

Using protractor you can do

element.all(by.tagName('span').get(0).getText().then(function(text){
    textTokens = text.split(" ");
    var Id = textTokens[textTokens - 1];
    console.log(Id);
});

Note if you have multiple spans you can put the above code in a loop and display the Id for each span element in your console.

element.all(by.tagName('span').count().then(function(spanCount){
    for (var i = 0; i < spanCount; i++){
        element.all(by.tagName('span').get(i).getText().then(function(text){
            textTokens = text.split(" ");
            var Id = textTokens[textTokens - 1];
            console.log(Id);
        });
    }
});

Upvotes: 0

Sudharsan Selvaraj
Sudharsan Selvaraj

Reputation: 4832

You can use regex to extract numbers from a string. Look at below example code.

var id = element(by.css("span.link")).getText().then(function(text){
  return text.replace(/[^0-9]+/g, "");
})

Upvotes: 3

Related Questions