网站使用casperjs或新浏览器登录时要求输入验证码
问题描述:
试图登录admin.booking.com
,并从预订页面中删除我酒店的所有预订。但事情是每当我使用CasperJS登录或从一个新的位置(新浏览器)登录时,它都会要求进行电话验证。它将预订页面重定向到电话验证页面。这里是我的代码网站使用casperjs或新浏览器登录时要求输入验证码
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
viewportSize: {width: 950, height: 950}
});
casper.userAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
casper.start('https://admin.booking.com/', function() {
//this.captureSelector('bhuwan1.png', 'body');
});
casper.then(function(){
this.evaluate(function(){
document.getElementById("loginname").value="1123123";
document.getElementById("password").value="*******";
});
});
casper.then(function(){
//$("button").eq(1).click();
this.click('button.btn-primary');
//this.wait(10000);
console.log("first wait for 10sec");
});
casper.then(function(){
this.wait(10000);
});
casper.then(function(){
var ses = this.getCurrentUrl().split("ses=")[1].split("&")[0];
console.log(ses);
//this.wait(10000);
console.log("first wait for 10sec");
var url = "https://admin.booking.com/hotel/hoteladmin/extranet_ng/manage/search_reservations.html?stay_to=2017-01-08&stay_from=2017-01-07&type=arrival&hotel_id=xxxxx&ses="+ses;
console.log(url);
this.evaluate(function(url){
var newDiv = document.createElement("div");
var newContent = document.createElement("a");
newContent.setAttribute("href",url);
newContent.innerText="button";
newDiv.appendChild(newContent);
document.body.appendChild(newDiv);
this.wait(10000);
},url);
});
casper.then(function(){
this.clickLabel("button",'a');
});
casper.then(function(){
this.wait(10000);
console.log("first wait for 10sec");
this.capture('stack15.png', {
top: 0,
left: 0,
width:1000,
height: 2000
});
this.captureSelector('bhuwan15.png', 'body');
});
casper.run();
如何避免手机验证页面和登录,如果充分利用熟悉的或旧的浏览器登录?
答
我建议你使用基于cookie的认证。 您需要找到保留会话所需的最小量的cookie。在此之后,你需要添加到您的脚本是这样的:
phantom.cookies = [{// an array of objects
'name' : 'theAuthCookie',
'value' : '<very long string>',
'domain' : 'www.wikifolio.com',
'path' : '/',
'httponly' : false,
'secure' : true,
'expires' : (new Date()).getTime() + (1000 * 60 * 60 * 43800) //5 years
},{ 'name' : 'DisclaimerCountryPopupV2',
'value' : 'de',
'domain' : 'www.wikifolio.com',
'path' : '/',
'httponly' : false,
'secure' : true,
'expires' : (new Date()).getTime() + (1000 * 60 * 60 * 43800) }]
参见:this issue
您还需要使用--cookies-file
选项:./casperjs --cookies-file=./my_file test.js >/dev/stdout
您需要使用--debug=true
命令行选项,如果有任何问题。这些回调也可能是有用的:
casper .on("error", function(msg){ this.echo("error: " + msg, "ERROR") }) .on("page.error", function(msg, trace){ this.echo("Page Error: " + msg, "ERROR") }) .on("remote.message", function(msg){ this.echo("Info: " + msg, "INFO") }) .on("resource.error", function(resourceError){ console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')'); console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString); });
如何使用的cookies文件选项是什么my_file – Bhuwan
'--cookies文件= /路径/到/ cookies.txt'指定用于存储持久性Cookie的文件名。 PhantomJS - [命令行选项](http://phantomjs.org/api/command-line.html)。 我也觉得,它可能不需要,或者你可以尝试它,而不设置'phantom.cookies'。 – 2017-05-27 13:00:12
没有设置phatom.cookies它只是将所有的cookie传输到cookies.txt。谢谢 – Bhuwan