我已经在 PhantomJS 中加载了一个页面(从 NodeJS 使用它)并且该页面上有一个 JS 函数 doRedirect()
其中包含
...
document.cookie = "key=" + assignedKey
document.location.reload(true)
我像这样从 PhantomJS 运行 doRedirect()
page.evaluate(function() {
return doRedirect()
}).then(function(result) {
// result is null here
})
我希望 PhantomJS 遵循 document.location.reload(true)
并返回该新页面的内容。如何做到这一点?
最佳答案
document.location.reload()
不会导航到任何地方,它会重新加载页面。这就像点击浏览器的刷新按钮。这一切都发生在前端,而不是发生 300 Redirect
的服务器。
只需调用该函数,等待 PhantomJS 完成页面加载,然后向其询问内容。
您可以使用 page.onLoadFinished
等待 PhantomJS 完成加载事件。此外,您可能需要在加载后使用 setTimeout()
来等待一些额外的时间让页面内容异步加载。
var webPage = require('webpage');
var page = webPage.create();
page.onLoadFinished = function(status) {
// page has loaded, but wait extra time for async content
setTimeout(function() {
// do your work here
}, 2000); // milliseconds, 2 seconds
};
关于javascript - 如何在 PhantomJS 中跟踪 document.location.reload?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40200531/