如何关注PhantomJS中的document.location.reload?

我在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()不会导航到任何地方,它会重新加载页面。 这就像点击浏览器的刷新button。 这一切都发生在前端,而不是发生300 Redirect的服务器。

只需调用该函数,等待PhantomJS完成加载页面,然后询问内容。

您可以使用page.onLoadFinished事件等待PhantomJS完成加载。 此外,您可能需要在加载后使用setTimeout() ,以等待页面内容asynchronous加载的额外时间。

 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 };