Подтвердить что ты не робот

Как войти на сайт с помощью CasperJS?

Как я могу войти в систему с помощью CasperJS, отправив форму. Я искал google и не нашел хороших примеров.

4b9b3361

Ответ 1

Вам понадобится функция Casper fill().

Ниже приведен пример входа в Facebook и распечатка имени после входа в систему. Обратите внимание, что вам нужно указать свое имя пользователя и пароль:

var casper = require('casper').create({   
    verbose: true, 
    logLevel: 'debug',
    pageSettings: {
         loadImages:  false,         // The WebPage instance used by Casper will
         loadPlugins: false,         // use these settings
         userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4'
    }
});

// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
});

// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
    this.echo("Page Error: " + msg, "ERROR");
});

var url = 'http://www.facebook.com/';

casper.start(url, function() {
   console.log("page loaded");
   this.test.assertExists('form#login_form', 'form is found');
   this.fill('form#login_form', { 
        email: '**<put your email here>**', 
        pass:  '**<put your password here>**'
    }, true);
});

casper.thenEvaluate(function(){
   console.log("Page Title " + document.title);
   console.log("Your name is " + document.querySelector('.headerTinymanName').textContent ); 
});

casper.run();

Ответ 2

Вот немного измененная версия кода от Ngo Hung. Селектор для имени пользователя был отключен, как и назначение userAgent:

var casper = require('casper').create({   
    verbose: true, 
    logLevel: 'debug',
    userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22',
    pageSettings: {
      loadImages:  false,         // The WebPage instance used by Casper will
      loadPlugins: false         // use these settings
    }
});

// print out all the messages in the headless browser context
casper.on('remote.message', function(msg) {
    this.echo('remote message caught: ' + msg);
});

// print out all the messages in the headless browser context
casper.on("page.error", function(msg, trace) {
    this.echo("Page Error: " + msg, "ERROR");
});

var url = 'http://www.facebook.com/';

casper.start(url, function() {
    console.log("page loaded");
    this.test.assertExists('form#login_form', 'form is found');
    this.fill('form#login_form', { 
        email: '**<put your email here>**', 
        pass:  '**<put your password here>**'
    }, true);
});

casper.thenEvaluate(function(){
    console.log("Page Title " + document.title);
    console.log("Your name is " + document.querySelector(".fbxWelcomeBoxName").innerHTML);
});

casper.run();

ОБНОВЛЕНИЕ. Я рекомендую кому бы то ни было использовать собственный сайт или, по крайней мере, не facebook.com, если они планируют большие объемы тестирования. Facebook мог сказать, что я пытался войти в систему снова и снова, начал отправлять мне по электронной почте и заставлял меня подтверждать свою учетную запись с помощью SMS.