Question

I have the following code in an exercise I've made for myself:

var imgs = page.evaluate(function() {
   return document.images;
});

for (var i in imgs){
    console.log("source: " + i.src);
}

but I only get multiple "undefined" messages.

When I try getAttribute('src'), I get:" 'undefined' is not a function... " error message.

I've verified the page has img elements with src attributes.

Solution:

I solved this by as suggested here by:

for (var i = 0; i < imgs.length; i++){
    if (imgs[i])
        console.log("source: " + imgs[i].src);
}

See this question for more information.

Was it helpful?

Solution

Try this:

for (var i = 0; i < imgs.length; i++) {
    console.log("source: " + imgs[i].src);
}

instead

for (var i in imgs){
    console.log("source: " + i.src);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top