单击以获得带回发的下一页

时间:2014-11-09 13:10:34

标签: javascript web-scraping casperjs

我正在尝试从带有回发的ASP页面中获取第1页和第2页的屏幕截图。 我正在尝试使用casperjs访问第一页并保存屏幕截图,然后单击下一步并从第二页截取屏幕截图。

我的代码适用于其他一些网站,但这个网站让我很难过。

我的代码如下:

var casper = require('casper').create({logLevel: "debug", verbose: true });
casper.start('http://www.slot.pt/JobVacancies', function() {
    this.capture('Step1.png');
    this.wait(5000, function() {
        this.click('#ctl00_ctl00_MainContent_MainContent_dvwListItems_PGB a.dxp-button.dxp-bi:nth-of-type(1)');
    });
});

casper.run(function() {
    this.capture('Step2.png');
    this.echo('finished');
    this.exit();
});

调试信息:

[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: http://www.slot.pt/JobVacancies, HTTP GET
[debug] [phantom] Navigation requested: url=http://www.slot.pt/JobVacancies, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://www.slot.pt/JobVacancies"
[debug] [phantom] Navigation requested: url=http://platform.twitter.com/widgets/tweet_button.d58098f8a7f0ff5a206e7f15442a6b30.pt.html#_=1415538373180&count=none&id=twitter-widget-0&lang=pt&original_referer=http://www.slot.pt/JobVacancies&size=m&text=S L O T - Ofertas de Emprego&url=http://www.slot.pt/JobVacancies, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/2 http://www.slot.pt/JobVacancies (HTTP 200)
[debug] [phantom] Capturing page to /home/netlisbon/test/Step1.png
[info] [phantom] Capture saved to /home/netlisbon/test/Step1.png
[info] [phantom] Step anonymous 2/2: done in 1559ms.
[info] [phantom] Step _step 3/3 http://www.slot.pt/JobVacancies (HTTP 200)
[info] [phantom] Step _step 3/3: done in 1560ms.
[info] [phantom] wait() finished waiting for 5000ms.
[debug] [phantom] Mouse event 'mousedown' on selector: #ctl00_ctl00_MainContent_MainContent_dvwListItems_PGB a.dxp-button.dxp-bi:nth-of-type(1)
[debug] [phantom] Mouse event 'mouseup' on selector: #ctl00_ctl00_MainContent_MainContent_dvwListItems_PGB a.dxp-button.dxp-bi:nth-of-type(1)
[debug] [phantom] Mouse event 'click' on selector: #ctl00_ctl00_MainContent_MainContent_dvwListItems_PGB a.dxp-button.dxp-bi:nth-of-type(1)
[info] [phantom] Done 3 steps in 6571ms
finished
[debug] [phantom] Capturing page to /home/netlisbon/test/Step2.png
[info] [phantom] Capture saved to /home/netlisbon/test/Step2.png

但是,这两个截图都显示了相同的页面...... 我试图点击另一个箭头转到最后一页,但我得到了相同的结果。

有人能在这里找出我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

回发功能是JavaScript,它不会触发导航步骤。但自从CasperJS'步骤只等待当前导航步骤完成,脚本在下一个"页面之前完成"装了。

点击

后,您必须等待一段静态时间
this.wait(5000, function() { this.click('selector'); });
this.wait(5000);

甚至更好,您可以跟踪您所在的页面并等待显示特定页面:

var page = 1;
this.wait(5000, function() {
    this.click('selector');
    page++;
});
this.waitFor(function check(){
    return this.fetchText(".dxp-summary").indexOf("Page " + page + " of ") === 0;
}, function then() {
    // screenshot or whatever
});