gpt4 book ai didi

node.js - Electron Nightmare.js Node 列表到数组

转载 作者:行者123 更新时间:2023-12-03 13:24:00 26 4
gpt4 key购买 nike

我正在尝试遍历通过 Nightmare.js 获得的 NodeList。在开发工具中执行按预期执行,但在 Electron 中我无法成功将 NodeList 转换为数组。

   nightmare
.goto('https://www.somePage.com')
.wait('#someID')
.evaluate(function () {
var links = document.querySelectorAll('div.someClass')
return links;
})
.end()
.then(function (result) {
console.log(result); // outputs the NodeList successfully.
var nodesArray = Array.prototype.slice.call(result);
console.log(nodesArray.length) // Always 0
})
.catch(function (error) {
console.error('Failed',
error);
})


我尝试通过各种其他方法移植 NodeList。在 Electron 中没有什么可以工作的。同样,这在 chrome 工具中很容易实现。

最佳答案

问题是 HTMLElements 或 Nodes 在页面上下文中是有效的。需要从 evaluate() 传递到 then() 的任何内容都使用 nightmare.ipc 模块在内部发送。这意味着返回的值被转换为字符串(JSON.stringify),然后再创建回来。

如果您检查开发人员控制台日志,那么您会看到转换错误。

您可以在评估函数本身中评估长度并传递它。

nightmare
.goto(url)
.evaluate(function(selector) {
var links = document.querySelectorAll(selector)
return links.length;
}, selector)
.then(function(result) {
console.log(result); // Outputs length.
})
.catch(function(error) {
console.error('Failed', error);
});

如果您需要在不同的评估()步骤中传递元素,那么您可以有解决方法,但这是不同的故事

关于node.js - Electron Nightmare.js Node 列表到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44249472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com