someone235
someone235

Reputation: 586

How to get the page title every time the url changes with CasperJS

I've checked the CasperJS documentation, and it seems that there's no event that gives me access to the current document when a navigation occurs. So is it possible to get the new title of a page every time there is a navigation?

Upvotes: 1

Views: 2000

Answers (2)

Grant Miller
Grant Miller

Reputation: 29037

Here's an ever-so-slightly faster version of the accepted answer that will make a significant difference if you are dealing with many URL changes:

casper.on('url.changed', function () {
  this.then(function () {
    console.log(this.page.title);
  });
});

Upvotes: 0

Artjom B.
Artjom B.

Reputation: 61932

url.changed is emitted as soon as another URL is loaded which usually means that the title has also changed. It seems it isn't emitted at the end of the page load so you also need to add a step for CasperJS to wait until the page is loaded.

casper.on("url.changed", function(){
    this.then(function(){
        this.echo(this.getTitle());
    });
});

You can use the navigation.requested event in exactly the same way.

Upvotes: 2

Related Questions