Closed ijx33 closed 11 years ago
Please read the documentation. You can only return primitive serializable object from evaluate
(no closure, DOM object, etc).
If you only need a specific subset of the elements' values you can workaround this restriction by using the following function for evaluation:
function() {
var imgTags = document.getElementsByTagName('img');
var ret = {};
for (var i = 0; i < imgTags.length; i++) {
try {
var imgProps = {};
imgProps['width'] = imgTags[i].width;
imgProps['height'] = imgTags[i].height;
imgProps['naturalWidth'] = imgTags[i].naturalWidth;
imgProps['naturalHeight'] = imgTags[i].naturalHeight;
imgProps['src'] = imgTags[i].src;
ret['tag' + i] = imgProps;
} catch (err) { }
}
return ret;
};
Hi Manuel! It is better to use getAttribute('name_of_attribute') inside your loop. I found that it does not work ok for attribute 'sizes', for tag 'link' at least. Also I checked it with some additional custom attribute on tag 'LINK'. Also did not work. It only could get the first attribute (rel) and that's all. Example of code with call to getAttribute() method:
function () {
var linkTags = document.head.getElementsByTagName('link');
var ret = {};
for (var i = 0; i < linkTags.length; i++) {
try {
var linkProps = {};
linkProps['rel'] = linkTags[i].getAttribute('rel');
linkProps['sizes'] = linkTags[i].getAttribute('sizes');
ret['tag' + i] = linkProps;
} catch (err) { }
}
return ret;
}
with getAttribute() it works ok.
@CunuuKum / @wilriker So is there anyway to just grab all/any H1 tags even if there's no assured ID, class or attribute?
document.getElementsByTagName('h1') see this: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByTagName also works on other elements for those enclosed inside.
That's built into the dom. Most people use jQuery as it's pretty powerful to get stuff.
Similar issue on Stackoverflow with a solution: https://stackoverflow.com/questions/44952239/process-dom-elements-with-phantomjs
When using evaluate to get response from the method document.getElementsByTagName it gives only the first element found, the response length is correctly given, but the later elements are all null. Same behaviour fro document.getElementsByClassName
Simple example:
(using 32bit windows)