gpt4 book ai didi

javascript - 为什么 Phantom.js 不评估我的 Backbone 应用程序的工作?

转载 作者:搜寻专家 更新时间:2023-10-31 23:06:16 25 4
gpt4 key购买 nike

page.open(My_url, function(status) {
page.evaluate(function() {
var html = document.documentElement.outerHTML;
return html;
}, function(result){
res.send(result);
});
});

我有一个 Backbone 应用程序。 “My_url”指向那个主干页面。

当我运行这段代码时,outerHTML 不包含呈现的 View 。它只有基本的 css/脚本/容器持有者。

就像 Backbone 应用程序根本没有运行一样。如何让 evaluate() 返回完全呈现的 Backbone 应用程序(在所有 ajax 调用和其他东西之后)?

毕竟,这就是 Phantom.js 的意义所在

最佳答案

在页面加载后立即对其进行评估并不总是足够的。有时,由于 JavaScript 在页面上的编写方式,您将不得不等待页面的 JavaScript 执行。为此,我建议您在执行评估之前尝试添加等待时间。例如

function ( status ) {
if ( status === 'fail' ) {
phantom.exit( 1 );
return; // essential, if you don't, below will be executed
}

window.setTimeout(
function () {
var result = page.evaluate(
function () {
var html = document.documentElement.outerHTML;
return html;
}
);
res.send( result );
},
1000 // wait 1,000ms (1s)
);
}

如果您发现有必要等待,那么您可能想要等待某事。为此,请在 Internet 上寻找 waitFor 或类似函数,它们将允许您的脚本持续轮询,直到出现特定的 DOM 元素。

关于javascript - 为什么 Phantom.js 不评估我的 Backbone 应用程序的工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16707425/

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