简体   繁体   English

CasperJS每次URL更改时如何获取页面标题

[英]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. 我已经检查了CasperJS文档,似乎没有事件可以让我在导航时访问当前文档。 So is it possible to get the new title of a page every time there is a navigation? 那么是否有可能在每次导航时获取页面的新标题?

url.changed is emitted as soon as another URL is loaded which usually means that the title has also changed. 一旦加载另一个URL,就会发出url.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. 似乎它在页面加载结束时没有发出,因此您还需要为CasperJS添加一个步骤,以等待页面加载完成。

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

You can use the navigation.requested event in exactly the same way. 您可以以完全相同的方式使用navigation.requested事件。

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: 这是接受答案的速度 快的版本,如果您要处理许多URL更改,将会有很大的不同:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM