javascript - 使用 casperjs 或新浏览器登录时网站要求输入验证码

标签 javascript phantomjs casperjs headless-browser

尝试登录admin.booking.com并从预订页面删除我酒店的所有预订。但问题是,每当我使用 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的身份验证。 您需要找到保持 session 所需的最少量 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

You also need to use the --cookies-file option:

./casperjs --cookies-file=./my_file test.js >/dev/stdout


You need to use --debug=true command-line option if there is any problem.

These callbacks may also be useful:

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);
});

关于javascript - 使用 casperjs 或新浏览器登录时网站要求输入验证码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41546985/

相关文章:

javascript - 交换内容后如何刷新div的表格宽度样式?

javascript - 如何用fabric.js画圆弧?

c# - 使用 AJAX 上传文件

javascript - Phantomjs/Casper 不会在 &lt;iframe&gt; 内渲染 <embed>

javascript - 如何将函数输出传递给另一个函数

javascript - 如何从我的应用程序中删除标题?为什么显示标题?

javascript - 自动化应用程序测试 (phantomjs) 和 merge 分支

javascript - Karma/Jasmine 规范 -- 预期 { } 等于 { }

javascript - Phantomjs 检查响应 header 然后执行某些操作

javascript - 在 TravisCI 上使用 casperJS 脚本时无法加载网页?