question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

element ... .getAttribute(...) returning Objects unexpectedly

See original GitHub issue

I’m collecting all of the anchor tags on a page with the intent of checking to ensure that they aren’t dead links… but I’m unexpectedly finding that I’m unable to get valid HREF values for half of them.

element.all(by.css('a')).each(function (element) {
    var linkTarget = element.getAttribute('href');
    expect(typeof linkTarget).toBe("string");
});

If I run code in the browser on the same site, I find that all of my anchor tags have an href of type “string”. Am I using the API incorrectly? I’m using Protractor on a non-Angular page: does this functionality depend on Angular?

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:19 (1 by maintainers)

github_iconTop GitHub Comments

19reactions
juliemrcommented, Apr 2, 2014

Your problem is that element.getAttribute() is returning a Promise. You want to do something like:

element.all(by.css('a')).each(function (element) {
    var linkTarget = element.getAttribute('href').then(function(attr) {;
        expect(typeof attr).toBe("string");
    });
});
3reactions
alediaferiacommented, Jul 28, 2015

Since pretty much everything in Protractor returns a promise I was wondering when is required to then and when it is not.

As per this excerpt from the web page, why is this OK

// You wrote your first test, cross it off the list
todoList.get(2).element(by.css('input')).click();
var completedAmount = element.all(by.css('.done-true'));
expect(completedAmount.count()).toEqual(2);

instead of the following?

// You wrote your first test, cross it off the list
todoList.get(2).element(by.css('input')).click().then(function() {
    var completedAmount = element.all(by.css('.done-true'));
    expect(completedAmount.count()).toEqual(2);
});

Do I have any guarantee that what is triggered by the click has finished executing by the time the rest of the code is executed?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript: getAttribute throwing unexpected identifier when ...
Try doing that in connectedCallback() function. Info: constructor() is called when the element is created.
Read more >
Element.getAttribute() - Web APIs | MDN
The getAttribute() method of the Element interface returns the value of a specified attribute on the element.
Read more >
what does Object GetAttributeValue( string name ) return?
No, by randomly I mean that the error sometimes occurs and sometimes not. However, when it occurs, it occurs for the same attribute...
Read more >
Attributes and properties - The Modern JavaScript Tutorial
The attributes collection is iterable and has all the attributes of the element (standard and non-standard) as objects with name and value ...
Read more >
Can't get value from field - Katalon Community
When I try to get the text from this field, it returns nothing. ... getAttribute(findTestObject('Object Id'), 'value') println result.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found