Javascript 如何在PhantomJS中跟踪document.location.reload?

Javascript 如何在PhantomJS中跟踪document.location.reload?,javascript,node.js,web-scraping,phantomjs,web-crawler,Javascript,Node.js,Web Scraping,Phantomjs,Web Crawler,我已经在PhantomJS中加载了一个页面(使用NodeJS),该页面上有一个JS函数doRedirect(),其中包含 ... document.cookie = "key=" + assignedKey document.location.reload(true) 我像这样从PhantomJS运行doRedirect() page.evaluate(function() { return doRedirect() }).then(function(result) { // resu

我已经在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重定向发生

只需调用该函数,等待PhantomJS完成页面加载,然后向其请求内容

您可以使用事件等待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
};

如何等待PhantomJS完成页面加载?我是否需要在
evaluate
函数中执行某些操作?请注意,此示例代码适用于普通的PhantomJS,而不是OP正在使用的node.js的PhantomJS桥。是的,该代码绝不是确定的,只是处理某些异步加载的不同方法的示例。