使用CasperJS接受cookie政策

时间:2016-08-27 16:01:32

标签: javascript web-scraping phantomjs casperjs

我一直在尝试使用CasperJS登录Marktplaats.nl (CraigsList-like webpage in Netherlands)。但是,我坚持接受cookie政策。

到目前为止,这是我的脚本:

var casper = require('casper').create({   
    verbose: true, 
    logLevel: 'debug',
    pageSettings: {
         loadImages:  false,         
         loadPlugins: false,       
         userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
    }
});

var x = require('casper').selectXPath;

casper.start('https://www.marktplaats.nl', function() {
    this.echo(this.getTitle()); //Prints "Marktplaats Cookiewall"
    //POINT1
    //Here I just check and log if the "Agree" button exists.
    if (casper.exists(x("//input[contains(@value, 'Cookies accepteren')]"))) {
        casper.echo("Agree button found");
    }
    else
    {
        casper.echo("Agree button not found");
    }
});

casper.then(function() {
    if(this.getTitle().indexOf("Cookiewall") !== -1)
    { 
     //POINT2           
     //If we are on the cookiewall page, click on agree. 
     casper.echo("Clicking on agree");
     casper.click(x("//input[contains(@value, 'Cookies accepteren')]"));
    }
});

casper.thenOpen('https://www.marktplaats.nl', function() {
//POINT3
//Reloaded page
this.echo('Second Page: ' + this.getTitle());
});

casper.run();

首先尝试导航到主页(在代码中标记为POINT1),但是会被重定向到要我接受cookie策略的Cookiewall页面。在浏览器中截取的屏幕截图,没有与CasperJS的连接。 Cookiewall page

在POINT2,我的脚本点击" Cookies接受者" - 记录为:

[debug] [phantom] Mouse event 'mousedown' on selector: xpath selector: //input[contains(@value, 'Cookies accepteren')]
[debug] [phantom] Mouse event 'mouseup' on selector: xpath selector: //input[contains(@value, 'Cookies accepteren')]
[debug] [phantom] Mouse event 'click' on selector: xpath selector: //input[contains(@value, 'Cookies accepteren')]

我是CasperJS的新手,但这对我来说很好。

最后,在POINT3,我重新加载主页,但再次被重定向到Cookiewall页面 - casper记录Cookiewall标题并记录重定向 评论后更新:我按照Artjom的评论注册了 resource.error,page.error,remote.message和casper.page.onResourceTimeout 。 2 ResourceErrors出现了。我相应地编辑了这个日志:

[info] [phantom] Step anonymous 3/5: done in 2018ms.
[debug] [phantom] opening url: https://www.marktplaats.nl/, HTTP GET
ResourceError: {
    "errorCode": 5,
    "errorString": "Operation canceled",
    "id": 7,
    "status": null,
    "statusText": null,
    "url": "http://s3.amazonaws.com/ki.js/56612/b7M.js"
}
[debug] [phantom] Navigation requested: url=https://www.marktplaats.nl/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=http://www.marktplaats.nl/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=http://www.marktplaats.nl/cookiewall
/?target=http%3A%2F%2Fwww.marktplaats.nl%2F, type=Other, willNavigate=true, isMa
inFrame=true
[debug] [phantom] url changed to "http://www.marktplaats.nl/cookiewall/?target=http%3A%2F%2Fwww.marktplaats.nl%2F"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 5/5 http://www.marktplaats.nl/cookiewall/?target
=http://www.marktplaats.nl/ (HTTP 200)
Second Page: ? Marktplaats - Cookiewall
[info] [phantom] Step anonymous 5/5: done in 2224ms.
[info] [phantom] Done 5 steps in 2243ms
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
ResourceError: {
    "errorCode": 5,
    "errorString": "Operation canceled",
    "id": 11,
    "status": null,
    "statusText": null,
    "url": "http://s3.amazonaws.com/ki.js/56612/b7M.js"
}
[debug] [phantom] url changed to "about:blank"

我似乎无法访问主页。

1 个答案:

答案 0 :(得分:1)

你需要做两件事:

  • 加载图片,因为在未加载图片时按钮没有尺寸。
  • 点击后稍等片刻。 CasperJS似乎没有发现页面加载正在发生。

完整脚本:

var casper = require('casper').create({   
    //verbose: true, 
    //logLevel: 'debug',
    pageSettings: {
         loadImages:  true,         
         loadPlugins: false,       
         userAgent: 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
    }
});

var x = require('casper').selectXPath;

var acceptBtn = x("//input[contains(@value, 'Cookies accepteren')]");

casper.start('http://www.marktplaats.nl', function() {
        this.echo(this.getTitle());
    })
    .waitForSelector(acceptBtn)
    .thenClick(acceptBtn)
    .wait(100)
    .then(function(){
        this.echo(this.getTitle());
    })
    .run();