BonJon
BonJon

Reputation: 799

How to get texts from an element?

I am trying to get the text of an element from protractor.

I have something like

var te = element.all(by.css('.menu li')).get(0).getText();

console.log(te)

html

 <ul class="menu">
        <li>Test first</li>
        <li>test second</li>
        <li>third</li>
 </div>

All I want is to get 'Test first' but my console log show me entire protractor object.

{ ptor_: 
   { controlFlow: [Function],
     schedule: [Function],
     setFileDetector: [Function],
     getSession: [Function],
     getCapabilities: [Function],
     quit: [Function],
     actions: [Function],
     touchActions: [Function],
     executeScript: [Function],
     executeAsyncScript: [Function],
     call: [Function],
     wait: [Function],
     sleep: [Function],
     getWindowHandle: [Function],
     getAllWindowHandles: [Function],
     getPageSource: [Function],
     close: [Function],
     getCurrentUrl: [Function],
     getTitle: [Function],
     findElementInternal_: [Function],
     findDomElement_: [Function],
     findElementsInternal_: [Function],
     takeScreenshot: [Function],
     manage: [Function],
     switchTo: [Function],
     driver: 
      { session_: [Object],
        executor_: [Object],
        flow_: [Object],
        fileDetector_: null },
  …..mroe

I also try

var te = element.all(by.css('.menu li')).get(0).getSize();

and

var te = element.all(by.css('.menu li')).first().getSize();

but still getting the entire object. Can anyone help me to solve this puzzle? Thanks a lot!

Upvotes: 0

Views: 91

Answers (2)

sombrerogalaxy
sombrerogalaxy

Reputation: 406

Probably all (public) functions in Protractor are promises, so to get the text of an element you would need to write the following code:

element.all(by.css('.menu li')).get(0).getText().then(function(text) {
    console.log(text);
});

The same structure applies to .getSize().

Only protractor's expect can resolve these promises automatically.

expect(myElement.getText()).toEqual("resolved");

Upvotes: 3

Rahul Kumar
Rahul Kumar

Reputation: 57

Try this JavaScript code:

x=document.getElementsByTagName("li");
te=x[0].innerHTML;

Now use the te to create the log.

Upvotes: 0

Related Questions