0
我一直在嘗試使用CasperJS登錄到Marktplaats.nl (CraigsList-like webpage in Netherlands)。但是,我堅持接受cookie政策。用CasperJS接受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。
在POINT2我的劇本上的 「Cookies accepteren」 點擊 - 因爲這是記錄:
[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頁面 - 卡斯帕記錄的Cookiewall標題和重定向登錄意見後 更新:我註冊resource.error,page.error,遠程.message和casper.page.onResourceTimeout根據Artjom的評論。 2個資源錯誤已經顯示出來。我相應地編輯了這個日誌:
[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"
我似乎無法進入主頁。
PhantomJS版本你使用哪種?請註冊到'resource.error','page.error','remote.message'和'casper.page.onResourceTimeout'事件([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file -2_caspererrors-JS))。也許有錯誤。 –
@ArtjomB。我註冊了這些活動。資源錯誤顯示。請檢查更新的問題的最後部分。謝謝。 – Nitkov
@ArtjomB。對不起 - PhantomJS 2.1.1 – Nitkov