Search code examples
javascripteventsphantomjscasperjs

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?


Solution

  • 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.