Why doesn't this.evaluate return DOM nodes correctly?

房东的猫 提交于 2019-11-27 05:41:02

It makes sense, because elmnt.options in the last snippet is an array full with undefined values. So you know the number of elements, but not their values. The reason is that DOM nodes cannot be passed from page context. The docs say:

Note: The arguments and the return value to the evaluate function must be a simple primitive object. The rule of thumb: if it can be serialized via JSON, then it is fine.

Closures, functions, DOM nodes, etc. will not work!

So either you do everything inside of the page context (evaluate) or you get a representation of the DOM nodes that you want to work with. Which I think is not what you want.

var elmnt = this.evaluate(function () {
    return [].map.call(document.getElementsByName("symbol")[0].options, function(option){
        return {text: option.innerText, value: option.value};
    });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!