listing all images #11207

Closed
jampow opened this Issue Apr 4, 2013 · 2 comments

Comments

Projects
None yet
2 participants

jampow commented Apr 4, 2013

I can't list all images with this code:

var imgs = page.evaluate(function(){ return document.getElementsByTagName('img'); });

for (var i = imgs.length - 1; i >= 0; i--) {
    console.log(imgs[i]);
}

It creates an array with the correct length, but only the position 0 has an image object the others positions got null. Is this a bug or am I doing something wrong?

Collaborator

JamesMGreene commented Apr 4, 2013

You can't return DOM objects to the PhantomJS context, they're bound to the page.

Depends on what you want to do... for example. you can easily return the various metadata you might care about from them:

var imgUrls = page.evaluate(function(){
  return [].slice(document.getElementsByTagName('img'), 0).map(function(img) {
    return img.href;
  });
});

for (var i = imgUrls.length - 1; i >= 0; i--) {
    console.log(imgUrls[i]);
}
Collaborator

JamesMGreene commented Apr 4, 2013

The only type of objects you can return are essentially objects that are JSON-serializable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment