user779159
user779159

Reputation: 9602

How to follow a document.location.reload in PhantomJS?

I've loaded a page in PhantomJS (using it from NodeJS) and there's a JS function on that page doRedirect() which contains

...
document.cookie = "key=" + assignedKey
document.location.reload(true)

I run doRedirect() from PhantomJS like this

page.evaluate(function() {
  return doRedirect()
}).then(function(result) {
  // result is null here
})

I'd like PhantomJS to follow the document.location.reload(true) and return the contents of that new page. How can this be done?

Upvotes: 3

Views: 1178

Answers (1)

Soviut
Soviut

Reputation: 91535

document.location.reload() doesn't navigate anywhere, it reloads the page. It's like clicking the refresh button your browser. It's all happening in the frontend, not the server, where 300 Redirect happens.

Simply call that function, wait for PhantomJS to finish loading the page, then ask it for the contents.

You can wait for PhantomJS to finish loading by using the page.onLoadFinished event. Additionally, you might need to use setTimeout() after load to wait some additional amount of time for page content to load asynchronously.

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

Upvotes: 1

Related Questions